Initialize Members (whose constructors accept arguments) in a struct in a class
I am receiving errors from my compiler when I try code with this general format:
in classname.hpp
#include "otherclass.hpp"
class classname {
public:
typedef struct {
otherclass membername(sometype);
} structname;
void functionname(structname& exampleData);
}
in main.cpp, I define a variable exampleData:
classname::structname exampleData;
in classname.cpp, I have a function that uses exampleData and attempts to initialize the member, membername, of the struct:
void classname::functionname(structname& exampleData){
sometype a;
exampleData.membername(a);
}
开发者_如何学GoI am receiving an error from my compiler stating:
Undefined symbols:
classname::structname::membername(sometype)
" referenced fromclassname::functionname(classname::structname&)
in classname.o. ld: symbol(s) not found
I think that my error lies within the line
otherclass membername(sometype)
I also thought it could be:
otherclass membername(sometype& a)
But I received the same errors.
I'm wondering if this is legal to do, to have a member in a struct in a class whose constructor is not (). In main.cpp, when I declare exampleData, membername has not been initialized yet. Is this legal or am I hunting in the wrong place?
The error you're getting looks a lot like a linker error caused by the function classname::structname::membername
not being defined anywhere. From the code you've posted, it looks like you've just declared the function without providing a definition anywhere, meaning that the code will compile but not link. You should be able to fix this by providing an implementation for the function somewhere in your program. It doesn't have anything to do with whether the parameter is taken by reference; in both cases you still need to provide an implementation.
精彩评论