Solved

Basic review of classes

Posted on 2000-03-27
7
177 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:SuperMario
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thankee Nietod, apparently it's good to go. =) Very helpful!

-Dan
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now