Talk About Network

Google





Programming > C - C++ Learning > How Do I List a...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 13 Topic 4076 of 4400
Post > Topic >>

How Do I List a Class in a Header?

by Hal Vaughan <hal@[EMAIL PROTECTED] > Mar 25, 2008 at 12:52 AM

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
 




 13 Posts in Topic:
How Do I List a Class in a Header?
Hal Vaughan <hal@[EMAI  2008-03-25 00:52:04 
Re: How Do I List a Class in a Header?
Hal Vaughan <hal@[EMAI  2008-03-25 01:10:17 
Re: How Do I List a Class in a Header?
Richard Heathfield <rj  2008-03-25 01:35:26 
Re: How Do I List a Class in a Header?
Jerry Coffin <jcoffin@  2008-03-24 19:46:47 
Re: How Do I List a Class in a Header?
Jerry Coffin <jcoffin@  2008-03-24 20:00:52 
Re: How Do I List a Class in a Header?
Hal Vaughan <hal@[EMAI  2008-03-25 04:26:15 
Re: How Do I List a Class in a Header?
"R. Scott Mellow&quo  2008-03-25 09:23:06 
Re: How Do I List a Class in a Header?
"R. Scott Mellow&quo  2008-03-25 11:03:17 
Re: How Do I List a Class in a Header?
Hal Vaughan <hal@[EMAI  2008-03-25 18:20:59 
Re: How Do I List a Class in a Header?
"R. Scott Mellow&quo  2008-03-25 14:41:08 
Re: How Do I List a Class in a Header?
Jerry Coffin <jcoffin@  2008-03-25 07:37:47 
Re: How Do I List a Class in a Header?
Bart van Ingen Schenau &l  2008-03-25 20:16:22 
Re: How Do I List a Class in a Header?
Hal Vaughan <hal@[EMAI  2008-03-26 04:43:34 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 10:37:50 PST 2009.