Solved

Basic review of classes

Posted on 2000-03-27
7
179 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 20 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

785 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