Solved

Basic review of classes

Posted on 2000-03-27
7
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 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