Solved

100 points: 1 class can't use another - #include gives link errs

Posted on 2003-11-21
4
262 Views
Last Modified: 2010-04-01
I am writing my my first ever serious Visual C++ 6 visual application. To give you a run down, i have created a default dialog program and created some extra dialogs.

I have core functional classes named Hobby, Client, DatingAgency (im sure you see where this is going)

To jump straight to the problem, if i try to use:    #include "Client.h"   i get the following error:
(this happens during linking)

WindowTestDlg.obj : error LNK2005: "public: __thiscall Client::Client(void)" (??0Client@@QAE@XZ) already defined in WindowTest.obj

This is repeated for every method of the Client object. If i don't use #include then i can't access the client class (undeclared identifier).

Now i deleted Client.cpp and wrote the declaration and the implementations in the header file, because im using #include <list> and using the STL List object.

Does anybody recognise this error? And know it's solution?

(extra info that may help: MS Visual C++ 6.0 Ent. Ed., Windows XP Pro)

(If you answer this corretly i will point you to a question that i posted for 70 points and never gave to anybody because i solved it myself. You can add a comment and i will give you those points as well.)

Thanks

Phill
0
Comment
Question by:Philluminati
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 30 total points
ID: 9799397
We'll probably need to see some code snippets.

In general, your .h files should

a) Contain the class definitions only - no code, thanks

b) Be protected against multiple #includ-ing, typically by something like
  #ifndef Client_H
  #define Client_H

  ... rest of the header file goes here

  #endif

which, I presume, VC++ puts in for you anyway.


Your .cpp files should contain the implemnentations of the classes etc.



It sound like you've got some implemenation, or some functions written in the .h files? Is that it?

Or, I guess, you've got the imlpemneation of the Client consturctor (Client::Client) in two .cpp files, namely WindowTest and WindowTestDialog.

You don't #include a .cpp anywhere do you? A .cpp should #include it's .h, and either a .cpp or .h can #include other .h files. Nothing should #include a .cpp anywhere.



Ah ! you say:
Now i deleted Client.cpp and wrote the declaration and the implementations in the header file, because im using #include <list> and using the STL List object.


So you have implemented Client in a .h file. That's not right, unless it's a template class itself. Implement in .cpp, define in .h


0
 

Author Comment

by:Philluminati
ID: 9799589
I don't believe it!!!

I'm shocked, my answers been correctly solved in less than an hour. Thats fantastic!

Thank-you very much, problems like that are a real pain. You know it's obvious but you don't have enough experience to know what it is. Thanks mate.

My end of the bargin: I promised you 100 points so copy and paste the path into address bar, leave a small comment and ill assign the 70 points there for you. (30 from this question).

http://www.experts-exchange.com/Applications/MS_Office/Outlook/Q_20790470.html#9700755

Thanks

Phillip Taylor
0
 

Author Comment

by:Philluminati
ID: 9799624
sorry - i never told you how i solved the problem. I added client.cpp back into the project and copy and pasted the definition in to its own file. Then it worked!

That easy

Thanks

Phill
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9809531
Phil..

You ought to ask in Community Support to close that other question and get your points back. Then post a new question in _this_ group, with a topic line of 'Points for andrewjb' and a reference to _this_ question. Then I'll comment in the new one, and you can give me the points.

Someone's going to be annoyed if you accept me as an answer in some unrelated Q, and you'll get told off by the admins!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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.

747 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