I'm having a problem with my code, even my professor has no idea why this is the case.
He wants me to define the structure with using Persontype= struct{}; and not a normal definition of a structure. I don't understand why or what is the difference.
he also don't want us to include the .cpp file in main.
Normal definition of a struct works fine but not with this.
the error
In file included from ...\LABOR1TEST\main.cpp:3:
...\LABOR1TEST\test.hpp:12:6: warning: 'void test(const std::vector<<unnamed struct> >&)' used but never defined
void test(std::vector<PersonType> const &_Personen);
^~~~
[100%] Linking CXX executable LABOR1TEST.exe
CMakeFiles\LABOR1TEST.dir/objects.a(main.cpp.obj): In function `main':
.../LABOR1TEST/main.cpp:12: undefined reference to `test(std::vector<._56, std::allocator<._56> > const&)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\LABOR1TEST.dir\build.make:120: LABOR1TEST.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:95: CMakeFiles/LABOR1TEST.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:102: CMakeFiles/LABOR1TEST.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:137: LABOR1TEST] Error 2
here is my code
main.cpp
#include <iostream>
#include <vector>
#include "test.hpp"
int main() {
PersonType p;
p.Name = "Max";
std::vector<PersonType> TEMPVEC;
TEMPVEC.push_back(p);
TEMPVEC.push_back(p);
test (TEMPVEC);
return 0;
}
test.cpp
#include <iostream>
#include <vector>
#include <string>
#include "test.hpp"
void test(std::vector<PersonType> const &_Personen)
{
for (auto const &i : _Personen)
{
std::cout << i.Name << std::endl;
}
}
and my test.hpp
#include <vector>
#include <string>
#ifndef LABOR1TEST_TEST_HPP
#define LABOR1TEST_TEST_HPP
using PersonType = struct {
std::string Name;
};
void test(std::vector<PersonType> const &_Personen);
#endif //LABOR1TEST_TEST_HPP
CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(LABOR1TEST)
set(CMAKE_C++_STANDARD 11)
add_executable(LABOR1TEST main.cpp test.cpp test.hpp)
My interpretation
using PersonType = struct { ..... };on a top-level will generate a different anonymous type in each translation unit (.cppfile, for simplicity) and then give it a name ofPersonTypein each translation unit. It does not matter that this statement happens insidetest.hpp, all#includes are handled by preprocessor and do not matter when dealing with types.So,
PersonTypein two translation units (main.cppandtest.cpp) actually refer to different types. In effect,main.cppexpects to findtest(vector<PersonType> const &)with onePersonType, andtest.cpponly providestest(vector<PersonType> const&)with anotherPersonType, hence the linkage error.Is it a GCC bug?
You can see the error better if you get rid of templates and try compiling the following two translation units together:
My GCC tells me the following:
However, replacing
using Foo = struct {};withtypedef struct {} Foo;apparently works, as well as switching from my GCC to my Clang.M.M from comments suggested that
typedefshould work because of [basic.link]/4.3It may get interpreted by GCC too literally(?) to exclude
using, although [dcl.typedef]/2 says that the name introduced byusingshould have the same semantics astypedef.Surprisingly, it was even asked on StackOverflow before!
Regarding your problem
If the text above is true, that's quite a strange requirement. It makes it impossible to link different translation units against
PersonType. I'm wondering what's the rationale behind, maybe you're not supposed to actually exposePersonTypebeyond a single translation unit? Consider consulting with your TA or a professor.