Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany
Accepted Solution

undefined reference to constructor of own class

I have some materials in subfolders of my project, and use the include with full path to add it:

#include <materials/dataservices/DataServiceConfig.hpp>

 this compiles well, but when i try to create the object i get this error:

undefined reference to `DataServiceConfig::DataServiceConfig(QObject*)'

 what am I doing wrong?

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: undefined reference to constructor of own class

Is there a warning about the include? Usually <blah.h> is for headers in the system path, and you would use "blah.h" for project headers.

 

Is DataServiceConfig in a namespace?

Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: undefined reference to constructor of own class

Hi Martin,
If i don't use the class but just the include, there is no warning and i can compile fine.
I don't use any namespaces (should I? Did not read about their concept yet)

I was not aware of the difference between #include<xxx> and #include"xxx", but unfortunately changing it does not resolve the issue.

Is there anything else i can provide? I could send you the full project Smiley Wink
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: undefined reference to constructor of own class

[ Edited ]

You don't need to worry about namespaces. Only reason I ask is because you can put classes in a namespace, but if you don't enable it or include it in the name, you can't see it.

 

But the "undefined reference" in an error usually means you can see the declaration in the header, but the definition wasn't compiled or linked by the compiler.

 

Some general times you'd see this:

1) you include and reference headers for a library but don't actually link the library at compile time with the -l<name> flag.

2) you can include the header, but the source isn't compiled/linked

3) the declare method arguments in the header differ from what is defined in the source

4) you don't prepend the classname on the method or constructor name in the source

 

So some things too look for:

 

* Are you using a Makefile project, or a managed project? (makes a difference on how the source file is included)

 

* Does the console output show your CPP file being compiled. (if not, it probably wasn't included. clean the project first to make sure you see the full source being built)

 

* If you are using a Makefile project and its not being included, do you explicitly include the file, or are you using a wildcard? I found with classes in subdirectories, just using a wildcard wasn't including the source files. If that is the case, check out this thread: http://supportforums.blackberry.com/t5/Native-Development/Alternative-to-listing-sources-headers-in-...

 

* If its a managed project, is the file extension .cpp? The eclipse plugin has a set list it looks for if others are specified.

 

If those don't help...

 

Another reason for undefined reference would be in the arguments in the method don't match. Usually the error message would suggest candidates that have the same method name, but different arguments.

 

Can you post:

* the full error message

* copy of where you declare the method in your class

* copy of where you define it in the source file (you can cut out the body.)

Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: undefined reference to constructor of own class

* I think it is a managed build, i did not change anything from the Momentics default.

 

* I don't see the file (or any other of my materials) mentioned in the build output. That means they are not included, correct?

So this is most likely the error. How do i change that? (and where!)

I used "properties, c/c++ build, paths and symbols, includes, gnu c, add, workspace" to add the three directories, following instructions from google (took me about an hour).

In "properties, c/c++ build" the checkbox "generate makefiles automatically" is not checked, but if i select it the build fails as well.

 

* Yes, the file extension is cpp.

 

Here is the declaration from the hpp file:

class DataServiceConfig : public QObject
{
    Q_OBJECT

public:
    DataServiceConfig(QObject* parent = 0);
    virtual ~DataServiceConfig();

 and here the (not very complex) part from the cpp:

#include "DataServiceConfig.hpp"

DataServiceConfig::DataServiceConfig(QObject* parent) {
	//void
}

DataServiceConfig::~DataServiceConfig() {
	// void
}

 

So i suspect i have to declare the usage of these subfolders somewhere?

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: undefined reference to constructor of own class

Usually this happens when you have this in your include file:

 

class MyClass

{

public:

    MyClass(MyThing*);

private:

    MyThing* m_thing;

};

 

but don't have this in your MyClass.cpp:

 

MyClass::MyClass(MyThing* thing) :

   m_thing(thing)

{

}

 

In other words, you have the declaration -- so the things that use your class compile -- but you don't have the implementation, so the linker complains that it can't find it.

 

Stuart

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: undefined reference to constructor of own class

[ Edited ]

Are you doing a cascades project? I had issues with Q_OBJECT in a managed build because it has to run some 'moc' command or something. ...thats probably an issue for another time though. (I ended up having to copy one of the sample projects with a PRO file because I couldn't figure out how to get a default empty project for it... it was stupid.)

 

Your files look fine.

 

You can test if its a subdirectory issue by just moving everything into the root and see if that compiles.

 

Momentics should've asked you if you wanted Makefile vs Managed when you created the project. If it is managed, and the files are under the source directory, subdirectories should be included automatically. I can't remember if there was a default src directory or not. If not, you should make sure that its marked as a source directory. The icon should be different. You can do add -> new source folder also. I don't have momentics on this machine to see the defaults.

Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: undefined reference to constructor of own class

I'll check in again after the weekend, good idea about moving the files.
Have to get my train now Smiley Happy
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: undefined reference to constructor of own class

[ Edited ]

smacmartin wrote:

In other words, you have the declaration -- so the things that use your class compile -- but you don't have the implementation, so the linker complains that it can't find it.

 

Stuart


It seems that this was the issue!

I removed the QObject-stuff from all classes, as they are just beans to store data, and also removed all constructor declarations.

The project compiles now, and i can also move the classes to the subfolders without any change, so that was not the issue.

Edit: Subfolders work only after putting them into the .pro file in the SOURCES/HEADERS section

 

If only the error messages would be a bit less general, searching for one returns a myriad of different issues that is really difficult to sort through.

 

Anyhow, back on track now!

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter