I think the mistake i’m doing it’s so stupid but i don’t know what i’m doing wrong.
I have a class with static and non-static vars and some methods, all public.
In my program i want to create an object and pass this object to a general method by reference.
The program doesn’t compile and the compiler throws really weird error messages.
Undefined symbols for architecture x86_64: “prueba::num”, referenced
from:
_main in ccbRZYqe.o
metodoC(prueba*) in ccbRZYqe.o
prueba::prueba()in ccbRZYqe.o
prueba::inicio() in ccbRZYqe.o “prueba::flag”, referenced from:
metodoC(prueba*) in ccbRZYqe.o
prueba::prueba()in ccbRZYqe.o
prueba::inicio() in ccbRZYqe.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
Code
#include <iostream>
using namespace std;
class prueba
{
private:
public:
static bool flag;
static int num;
float complejo;
// Metodos
//--------------
prueba()
{
flag = false;
num = 0;
complejo = 0.0;
}
void inicio()
{
flag = true;
num = 5;
complejo = 3.2;
}
bool cambio()
{
flag++;
num++;
complejo++;
}
};
bool metodoC(prueba* ensayo)
{
cout << "-----------------------------------------" << endl;
cout << "- flag: " << ensayo->flag << endl;
cout << "- num: " << ensayo->num << endl;
cout << "- Complejo: " << ensayo->complejo << endl;
cout << "-----------------------------------------" << endl;
return true;
}
//-----------------------------------
// M A I N
//-----------------------------------
int main(int argc, char *argv[])
{
prueba test;
test.inicio();
test.num += 2;
test.complejo += 5.2;
metodoC( &test );
return 0;
}
You need to define your static members. They are only declared.
and in the implementation file:
Note that you shouldn’t put the definitions in a header because you will get a definition of the statics for each translation unit. You need to put them in an implementation file that is then used to make a single object file that clients can build against.
Be careful though, each time you instantiate a new
pruebaobject, you reset the static members in the constructor.