Beginner class usage: splitting .h files

Dear members, please help with the following problem (it's on a beginner level).
I made a program that works, it uses a class. Then the book demonstrated how
to split the cpp file into one cpp and another one .h with the class, I managed to get it to work too. Then, the book demonstrated a technique of prototyping the class functions in a chocolate.h (chocolate is the class name) file, defining the class functions in a chocolate.cpp file. The class is used in the program hello.cpp

I tried to compile chocolate.cpp and then hello.cpp. chocolate.cpp compiled with a warning ("no start address") but hello.cpp would not compile. I use Digital Mars, here is the output from the compiler:
Error 42: Symbol Undefined ?getclassdata@chocolate@@QAE?AV?$basic_string@std@DV
?$char_traits@std@D@1@V?$allocator@std@D@1@@std@ (basic_string<>::d::DV?$char_tr
aits::std::D::chocolate syscall chocolate::getclassdata(allocator<>::d::D::choco
late ))
hello.obj(hello)
Error 42: Symbol Undefined ??0chocolate@@QAE@V?$basic_string@std@DV?$char_trait
s@std@D@1@V?$allocator@std@D@1@@std@ (syscall chocolate::chocolate(basic_string<
>::d::DV?$char_traits::std::D::basic_string<> ,allocator<>::d::D::basic_string<>
))

--- errorlevel 2


And now, my code.
hello.cpp:

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

int main ()
{ string taste_a;
chocolate inst1 ("white");
chocolate inst2 ("dark");
cout << inst1.getclassdata();
cout << inst2.getclassdata();
return 0; }
__________________________________
chocolate.h:

#include <string>
using namespace std;

class chocolate
{ public:
chocolate (string);
void setclassdata (string);
string getclassdata ();
private:
string c_taste;
};
________________________________
chocolate.cpp:

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

chocolate::chocolate (string taste_p)
{ setclassdata (taste_p); }

void chocolate::setclassdata (string taste_p)
{ c_taste = taste_p; }

string chocolate::getclassdata ()
{ return c_taste; }
______________________________

Your help will be much appreciated!

I've never used Digital Mars, but how it usually works with IDEs is you create a "project", and add all of these files to the same project. You don't compile each file individually. Instead, you build the entire project.

What it sounds like is you're having hello.cpp and chocolate.cpp each in their own project. That won't work because that implies they each are their own program... when really, they must be part of the same program.
thanks disch. dm is actually not an IDE. I am very simple minded and the rough command-prompt interface of dm does it for me. (I actually had to play with the files included with DM in order to get it to run standard library stuff!)

do you see any problems with the codes themselves?

The book I am reading fails to explain how the 3 files are linked.
Last edited on
I now understand that I should create an .obj with the chocolate.cpp and chocolate.h files and then link that to the hello.obj.

I need to figure out how to do it with DM.

Please just have a looksie at the code and if you see no errors, I will understand that this is a compiler issue...
dm is actually not an IDE. I am very simple minded and the rough command-prompt interface of dm does it for me.


Screw that. Get an IDE. Manually compiling and linking your programs is a huge job, especially as your program grows to multiple files. Plus IDEs (and hell, even makefiles) let you shorten the compiling process by only recompiling files that have changed since the last build. When you have 20+ source files (not atypical in a moderate sized program), this makes a big difference.

Don't be a masochist. Don't make the building process difficult just because you want to be a minimalist. There's nothing wrong with using an IDE.

do you see any problems with the codes themselves?


No. Your code builds without errors on my machine.
thank you again, disch!

please help me find an IDE that's free and easy to use for a "hello world"'er like me :D
you are right, my last question was off topic and pretty trivial. sorry for that.

I'll mark it as problem solved since my code is perfect :D
Topic archived. No new replies allowed.