UNDEFINED REFERENCE TO CLASS - SOLVED

Mar 25, 2017 at 11:27pm
Hello everyone, I am not sure if this is the right section for my question.
I'm sure you've seen a million questions like mine, but unfortunately I have the following problem.
I tried to write a program in C++ and when I try to run, it comes out the following message: "UNDEFINED REFERENCE TO CLASS" and I cannot compile.
The curious thing is that this problem appears only when I try to compile and run for the FIRST time, because when I close the editor and I reopen it, it works successfully.
So I think this problem has something to do with my compiler, therefore I would ask which compiler I should use for C++, Windows 64.
Or, maybe, if you think the problem is referred to the code, I could link my project folder, if you need.
Thanks to everyone guys!
Last edited on Mar 26, 2017 at 12:33pm
Mar 26, 2017 at 12:22am
It's very likely you got a more descriptive error message than "undefined reference to class".
Post your code, please, and the error message.

The LLVM/Clang compiler (clang++) is a good choice for Windows. The Microsoft C++ compiler CL.EXE is also fine.
Mar 26, 2017 at 9:30am
I wanted to add the code as attachment but I didn't find the option, I hope you will understand.


main.cpp

#include <iostream>
#include "provaClass.hpp"

using namespace std;

int main()
{
cout << "Hello world!" << endl;

provaClass provaObject;

provaObject.print();

return 0;
}




provaClass.cpp


#include "provaClass.hpp"
#include <iostream>
using namespace std;

provaClass::provaClass()
{
//ctor
}

provaClass::~provaClass()
{
//dtor
}

void provaClass::print()
{
cout << "stampa da classe";
}




provaClass.hpp


#ifndef PROVACLASS_H
#define PROVACLASS_H
#include <iostream>

class provaClass
{
public:
provaClass();
virtual ~provaClass();

void print();

protected:
private:
};

#endif // PROVACLASS_H



The error messages, referred to the main, are:

undefined reference to `provaClass::provaClass()'
undefined reference to `provaClass::print()'
undefined reference to `provaClass::~provaClass()'
undefined reference to `provaClass::~provaClass()'
Mar 26, 2017 at 12:04pm
Hello everyone! I think to understand why I had this problem.
I am using CodeBlocks 13.12 and when I create a class, there is a default setting that says

"Header and implementation files shall always be in the same folder"

This setting puts in the project folder the .h/.hpp and the .cpp files together, but maybe the compiler is not able to find them.
So the right way to solve the problem is

TO UNCHECK THE BOX "HEADER AND IMPLEMENTATION FILES SHALL ALWAYS BE IN THE SAME FOLDER",

so in the project folder two folders will be created:
1_ include (folder) in which there will be the ..hpp file
2_ src (folder) in which there will be the .cpp file
And in this ways, it works finally!

Thanks to mbozzi! I hope to be useful
Last edited on Mar 26, 2017 at 12:05pm
Topic archived. No new replies allowed.