Click here to Skip to main content
15,888,579 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: LNK2005 error - first program with classes Pin
Chris Losinger13-Aug-06 4:45
professionalChris Losinger13-Aug-06 4:45 
GeneralRe: LNK2005 error - first program with classes Pin
senthryl13-Aug-06 5:07
senthryl13-Aug-06 5:07 
GeneralRe: LNK2005 error - first program with classes [modified] Pin
Chris Losinger13-Aug-06 5:14
professionalChris Losinger13-Aug-06 5:14 
GeneralRe: LNK2005 error - first program with classes [modified] Pin
senthryl13-Aug-06 5:20
senthryl13-Aug-06 5:20 
GeneralRe: LNK2005 error - first program with classes Pin
Chris Losinger13-Aug-06 5:31
professionalChris Losinger13-Aug-06 5:31 
GeneralRe: LNK2005 error - first program with classes Pin
senthryl13-Aug-06 5:44
senthryl13-Aug-06 5:44 
GeneralRe: LNK2005 error - first program with classes Pin
Chris Losinger13-Aug-06 8:41
professionalChris Losinger13-Aug-06 8:41 
GeneralRe: LNK2005 error - first program with classes Pin
Mike Dimmick13-Aug-06 5:28
Mike Dimmick13-Aug-06 5:28 
The C++ model is that any function can be implemented in any translation unit, so long as there are not multiple definitions: that's what the LNK2005 error is telling you, that the same functions were defined more than once.

A 'translation unit' is the standardese for the result of preprocessing a given input. It's given this odd name because the input doesn't necessarily have to be a file - some C++ compilers will accept input from standard input or another redirected stream, although of course in practice this isn't very useful.

It's worth understanding that the #include statement simply pastes - includes - the contents of the referenced file into the current file at that point. This is the source of the multiple inclusions problem - that you can get 'multiply defined symbol' errors if you include a file more than once in any given translation unit. The traditional workaround is to wrap the whole 'header' file like this:
#ifndef __UNIQUE_SYMBOL__
#define __UNIQUE_SYMBOL__
 
// file contents here
 
#endif // __UNIQUE_SYMBOL__
You change __UNIQUE_SYMBOL__ for every new header file, so that each file has a unique definition. This so-called '#ifdef guard' effectively removes the contents of the file from the second definition. Microsoft's compilers since Visual C++ 4.0 support #pragma once which simply tells the compiler not to include this file again if it is encountered again, which is a little quicker than opening the file and noticing that the macro at the top is already defined. This feature is now supported by a number of other compilers.

In order to use a class, structure, or enumeration in a given translation unit, you must declare it. Traditionally this is done by including a so-called header file (traditionally given the .h extension although you might see .hpp or .hxx for C++ headers, but it can have any extension you like, or none in the case of the C++ standard library headers). You can simply redeclare the class in the second file but you can then have problems if the declarations get out of sync, which is why the #include mechanism exists.

It's best to think of each translation unit - each .cpp file - as being compiled completely independently from all the others, although the Microsoft compiler actually can work more efficiently with a batch of source files all specified on the same command line, and particularly with the Pre-Compiled Headers feature.

If you only refer to the class with a pointer (or reference), and never dereference the pointer (or reference), you can simply forward declare it, for example:
class MyClass;
You have to use the right keyword, so if it's declared as a struct you must forward-declare it as a struct.

If you define a new class using Visual Studio's wizards, it creates a header and a single .cpp file matching the class's name, but this is simply convention - you can define as many classes in a single header as you like, and you can implement multiple classes in a single file or a single class in multiple files, as you prefer. You might split a class into multiple files if the implementation is very large, to speed up compiles - the compiler will only recompile those files that have actually changed. You might be advised to reduce the number of methods in the class if that occurs, however. It is generally considered best practice to declare only one class in a header and implement it in a single source file, unless there are good reasons not to.

Stability. What an interesting concept. -- Chris Maunder

QuestionGetting friend member functions to work Pin
Joel Becker13-Aug-06 2:11
Joel Becker13-Aug-06 2:11 
AnswerRe: Getting friend member functions to work Pin
Mike Dimmick13-Aug-06 5:30
Mike Dimmick13-Aug-06 5:30 
GeneralRe: Getting friend member functions to work Pin
Joel Becker13-Aug-06 7:13
Joel Becker13-Aug-06 7:13 
AnswerRe: Getting friend member functions to work Pin
Bartosz Bien13-Aug-06 11:11
Bartosz Bien13-Aug-06 11:11 
GeneralRe: Getting friend member functions to work Pin
Joel Becker13-Aug-06 11:22
Joel Becker13-Aug-06 11:22 
GeneralRe: Getting friend member functions to work [modified] Pin
Bartosz Bien13-Aug-06 11:47
Bartosz Bien13-Aug-06 11:47 
GeneralRe: Getting friend member functions to work Pin
Joel Becker13-Aug-06 12:22
Joel Becker13-Aug-06 12:22 
QuestionString problem [modified] Pin
73Zeppelin13-Aug-06 1:28
73Zeppelin13-Aug-06 1:28 
GeneralRe: String problem Pin
ovidiucucu13-Aug-06 2:40
ovidiucucu13-Aug-06 2:40 
QuestionHow to print the text in the new line of a multi line edit box . Pin
VCSharp00713-Aug-06 1:02
VCSharp00713-Aug-06 1:02 
AnswerRe: How to print the text in the new line of a multi line edit box . Pin
ovidiucucu13-Aug-06 2:44
ovidiucucu13-Aug-06 2:44 
GeneralRe: How to print the text in the new line of a multi line edit box . Pin
Bram van Kampen13-Aug-06 14:54
Bram van Kampen13-Aug-06 14:54 
QuestionProblem in using GDI+ Pin
Dhananjayak0212-Aug-06 23:44
Dhananjayak0212-Aug-06 23:44 
AnswerRe: Problem in using GDI+ Pin
Stick^13-Aug-06 0:38
Stick^13-Aug-06 0:38 
Questionplease help Pin
jitun_vcpp12-Aug-06 20:56
jitun_vcpp12-Aug-06 20:56 
JokeRe: please help Pin
ovidiucucu13-Aug-06 2:47
ovidiucucu13-Aug-06 2:47 
AnswerRe: please help Pin
Hamid_RT13-Aug-06 9:46
Hamid_RT13-Aug-06 9:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.