This would seem quite basic, but the tutorials I've been looking at don't
cover it. They cover a lot of related aspects, but not this part itself.
Basically, I want to know how I should list a class in a header. I know,
from this group, to not include the .cpp files, so I list the functions in
a file as prototype functions in a .h file and use that instead. Now that
I'm using objects, I'm having trouble referencing an object from another
file. I have three files, test.cpp, test.h, and base.cpp. I have a class
in test.cpp and it's described in test.h and used in base.cpp, but I can't
get it to work quite right.
All the resources I've found tell me how to create cl*****, use
constructors, and so on, but nothing seems to cover what I need to do with
a class in a header file.
Here's my three files, first test.cpp, which has the class in it:
#include <cstdlib>
#include <iostream>
class NewClass {
public:
void testprint(std::string);
};
void NewClass::testprint(std::string s) {
std::cout << "Testing class stuff, string param: " << s <<
std::endl;
return;
}
//main() is only here because g++ insists it be here.
int main() {
return 0;
}
-------------
Here's test.h:
#include <iostream>
class NewClass {
public:
void testprint(std::string);
};
-------------
And here's base.h, which uses the class and depends on the header file:
#include <cstdlib>
#include <iostream>
#include "test.h"
int main() {
NewClass nc;
nc.testprint("test string");
return 0;
}
------
I can compile test.cpp fine but when I compile base.cpp, I get:
/tmp/ccgjOxzJ.o: In function `main':
base.cpp:(.text+0xb7): undefined reference to
`NewClass::testprint(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >)'
collect2: ld returned 1 exit status
My guess is that I'm not specifying info about the class correctly in
test.h. Is that right? What do I need to do to make it work properly?
Thanks!
Hal


|