class header issue

hi everybody
i have trouble in classes and headers . here is my files:

file:test.h

#ifndef TEST_H
#define TEST_H
using namespace std;

class CRectangle {
int x, y;
public:
void set_values (int,int);
int area ();
};

#endif // TEST_H



file test.cpp

#include "test.h"
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
int CRectangle::area()
{
return (x*y);
}




file: main.cpp

#include <iostream>
#include "test.h"
using namespace std;

int main () {
CRectangle rect;
rect.set_values (8,4);
cout << "area: " << rect.area();
return 0;
}

and i have this error:
undefined reference to `CRectangle::set_values(int, int)
undefined reference to `CRectangle::area()


when i use #include "test.cpp" instead of #include "test.h" in my main file everything is ok. can anybody help me?
Last edited on
Don't include test.cpp. You were doing it correctly in the code you posted.

You need to make sure test.cpp is linked with main.cpp. How this is done depends on what IDE you're doing, but usually you just have to make sure that both cpp files are part of the project.

Which IDE are you using? Visual Studio?
i dont know how i should link test.cpp with main.cpp
i have written in codeblocks
every three test.cpp test.h main.cpp is in the same directory and i have make test.h and test.cpp with codeblocks.
Topic archived. No new replies allowed.