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
Solved

Basic review of classes

Posted on 2000-03-27
7
180 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

809 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