10

I'm a Java developer experimenting with C++.

I just created a new class. In my other class I want to have list where I can store Filter objects.

Filter.h

#ifndef FILTER_H_
#define FILTER_H_

class Filter {
public:
  Filter(int id);
  int id;
  ~Filter();

};

#endif /* FILTER_H_ */

Filter.cpp

#include "Filter.h"

Filter::Filter(int id) {
this.id = id;
}
Filter::~Filter() {
}

Cars.h

#include "Filter.h"
...
...
private:
  std::vector<Filter> filters;

Cars.cpp

so in a function here I try to do this:

int id = 2;
Filter *filter = new Filter(id);

which generate this error:

Cars.cpp:120: undefined reference to `Filter::Filter(int)'
stl_construct.h:83: undefined reference to `Filter::~Filter()'

What's the reason for this?

9
  • This will not even compile. Are you sure, this is the real code? If so, you're compiling the project in a wrong way. How you do this? Aug 3, 2012 at 8:40
  • 2
    can you post your g++ build instructions? Aug 3, 2012 at 8:41
  • 8
    If you're serious about C++, please pick up a good book and learn it properly. The way you're going, you'll end up with a horrible, leaking, buggy java-like mess. C++ is not the same as Java.
    – Kerrek SB
    Aug 3, 2012 at 8:43
  • 5
    Please, please get a good book on C++ and forget everything you know from Java. C++ values completely different things, and you'll only hurt your C++ experience if you try to program the same way as you did in Java.
    – Xeo
    Aug 3, 2012 at 8:44
  • 3
    @Ole-M: Your specific problem is because of the way you are building your code. If you were building it the right way you would not be getting the errors above. The actual problem seems to be that you are not including Filter.cpp in your build. Unlike Java every C++ compiler is different. You need to learn how to use the compiler you have.
    – john
    Aug 3, 2012 at 8:52

1 Answer 1

26

The error is generated by the linker because it can not see where the definition of the constructor is located.

If you are using an IDE, you should add both .cpp files to the project so that they can be compiled together and the definition would be found by the linker.

If not, then you have to combine them yourself -assuming you are using gcc:

g++ cars.cpp filter.cpp

will combine them into one executable and should not show you that error

4
  • I have a Server.h file inside header sub directory and a Server.cpp file inside source sub directory. and the main.cpp is inside main directory. how to compile and create executable from main.cpp file?
    – Shahriar.M
    Jul 13, 2022 at 15:21
  • 1
    @Shahriar.M Same answer: If using IDE - add all .cpp files to the project. If using g++ and you are located inside the main directory: g++ source/Server.cpp main.cpp. You must have a main() function somewhere to get an executable - most likely in the file main.cpp. Also, the header files should be visible from the .cpp files. You could add the header directory to the g++ command using the flag -I, like this: -I ./header. Jul 15, 2022 at 11:30
  • I'm using VScode in ubuntu. this is a part of my Makefile before creating header and source subdirectories: tcp_utils.o: tcp_utils.h cserv.cc $(CXX) $(CXXFLAGS) -c -o cserv.o cserv.cc cserv: cserv.o tcp_utils.o $(CXX) $(CXXFLAGS) -o cserv cserv.o tcp_utils.o. (sorry for the bad format.) how to put your command inside Makefile?
    – Shahriar.M
    Jul 15, 2022 at 14:02
  • @Shahriar.M I think you should post that as a new question. This conversation no longer seems related to this particular answer and is getting too long to discuss in the comments. Jul 17, 2022 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.