Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Basic review of classes

Posted on 2000-03-27
7
Medium Priority
?
187 Views
Last Modified: 2010-04-02
Okay...

I don't know what's wrong but I can't get something to compile. I keep getting linker errors:

Undefined symbol class::function(datatype argument) in mainfile.cpp

My class declaration file has all the headers and no implementation. It looks like

#ifndef BLAH_H

class blah
{
   public:
      stuff;
   private:
      more stuff;
};

#define BLAH_H
#endif

My implementation file looks like:

#include "blah.h"

blah::blah()
{
   // construct
}

My question is: How does blah.h know that its implementation is in blah.cpp? Do I need to include something?

-Dan

0
Comment
Question by:SuperMario
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 80 total points
ID: 2661175
>> How does blah.h know that its implementation is in blah.cpp?
This is not specified by the C++ standard.   It varies from compiler to compiler.  Most modern GUI compilers have a concept called a "project" that creates an executable.  and all the .cpp files used to create the executable must be included in the project.  Older compilers often use makefiles the makefiles would compile the .cpp files and then link together all the object files produced by the compiler.
0
 
LVL 2

Expert Comment

by:danelroisman
ID: 2661181
Answer:

The solution of this trick is write
implementation in the header file into the body of the class, couse if you write #define BLAH_H in the header, the BLAH_H is alredy defined in cpp file!

Daniel
0
 
LVL 22

Expert Comment

by:nietod
ID: 2661458
That is ONE solution, not the only solution.  It is not a good choice for very large projects as it can make compile times unbearable.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:SuperMario
ID: 2661937
So all I have to do is create a project? I had to switch from MSVC++ to Borland at work and I never use it, so it was a really weird feeling to get a whole list of undefined member functions.

Whew.

-Dan
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2662104
creating a project is just the way you tell the IDE (development environemnt)'s what files belong together.

It then knows which .cpp files to compile int oobject (.obj) files.  And the linker then knows which .obj files (and libraries) to link to make an exe.

If you don't use a project (or you just use a command line compiler), then you need to manually build all the appropriate cpp files and link the approriate obj and library files.  Often this can be done by creating a text file for your linker input.  Better (if you have one) is to use a MAKE program that keeps track of what needs building (very much like a project does).

Also, if you're using the commandline, ensure you first read and understand how the compiler and linker work, and what all the commandline switches mean.

If you don't want all that hassle, then use a development environment (VS or Borland etc) that does all the hard work for you.

Certainly in VS (haven't used Borland) you can create all sort of starting projects from simple command line apps to full MFC/Windows apps etc.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2662252
The borland BC and BCB environments aren't all that different from the VC envirnoment.  Especially in this respect.  They all have projects, or projets workspaces, and you simply include ("add") all the files of the project into the workspace  These files should be all the .cpp files that get compiled and linked together, and any resource files (.res or .rc) that should be include, and any object files you want linked in (lib files or .obj).  They all work the same in this respect.
0
 
LVL 3

Author Comment

by:SuperMario
ID: 2662355
Thankee Nietod, apparently it's good to go. =) Very helpful!

-Dan
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question