Is there anyone can help me please!

Started by
10 comments, last by jbadams 17 years, 1 month ago
Hi guys! I am pretty new to C++, i am doing a simple program which makes use of inheritance! and I get this error (about the linker) I have done hundred time for searching information in able to sort out the problem but evetually i could not sort it out! help me please Main error LNK2005: "public: void __thiscall Room::Display(void)" (?Display@Room@@QAEXXZ) already defined in Main.obj Main error LNK2005: "public: __thiscall ClassRoom::ClassRoom(long,long,char,long,char)" (??0ClassRoom@@QAE@JJDJD@Z) already defined in ClassRoom.obj Main error LNK2005: "public: void __thiscall ClassRoom::ClassRoomExtra(void)" (?ClassRoomExtra@ClassRoom@@QAEXXZ) already defined in ClassRoom.obj Main error LNK2005: "public: __thiscall ClassRoom::ClassRoom(long,long,char,long,char)" (??0ClassRoom@@$$FQAE@JJDJD@Z) already defined in ClassRoom.obj Main error LNK2005: "public: void __thiscall ClassRoom::ClassRoomExtra(void)" (?ClassRoomExtra@ClassRoom@@$$FQAEXXZ) already defined in ClassRoom.obj Main error LNK2005: "public: __thiscall Room::Room(long,long,char)" (??0Room@@QAE@JJD@Z) already defined in Main.obj Main error LNK2005: "public: __thiscall Room::Room(long,long,char)" (??0Room@@$$FQAE@JJD@Z) already defined in Main.obj Main error LNK2005: "public: void __thiscall Room::Display(void)" (?Display@Room@@$$FQAEXXZ) already defined in Main.obj Main fatal error LNK1169: one or more multiply defined symbols found And here is the code #include <iostream> #include "stdafx.h" #include "Room.cpp" #include "ClassRoom.cpp" using namespace std; int main() { ClassRoom A1(1, 4,'M',200,'W'); char command; cout << "\n******Please Carefully Choose One Of The Following Option*******\n\n" <<"Press C.......(class room)" <<"\n......S.......(staff room)" <<"\n......L.......(lab room)" <<"\n......M.......(meeting room)" <<"\n......D.......(meeting room & class room)" <<"\n......Q....(quit)" <<"\nEnter Your Command Please -->"; cin >>command; while (command !='q') {switch(command) { case 'c': A1.Display(),A1.ClassRoomExtra() ; break; case 's': cout<<"hi2"; break; case 'l': cout<<"hi3"; break; case 'm': cout<<"hi4"; break; case 'd': cout<<"hi5"; break; } cout << "\n******Please Carefully Choose One Of The Following Option*******\n\n" <<"Press C.......(class room)" <<"\n......S.......(staff room)" <<"\n......L.......(lab room)" <<"\n......M.......(meeting room)" <<"\n......D.......(meeting room & class room)" <<"\n......F....(To find a Room)" <<"\n......Q....(quit)" <<"\nEnter Your Command Please -->"; cin >>command; } return 0; system("PAUSE"); } #pragma once class Room { protected: long floor; long number; char building; public: Room (long = 0, long = 0, char = ' '); void Display(); }; #include <iostream> #include "stdafx.h" #include "Room.h" using namespace std; Room::Room(long numb, long fl, char build) { fl = floor; numb = number; build = building; } void Room::Display() { cout << "Room number is " << number<<" At Floor "<<floor<<" In Building "<< building<< endl; } #include <iostream> #include "stdafx.h" #include "ClassRoom.h" using namespace std; ClassRoom::ClassRoom(long numb, long fl, char build, long ca, char type) :Room(fl, numb, build) { ca = capacity; type = BoardType; } void ClassRoom::ClassRoomExtra() { cout <<" Capacity is "<<capacity<<"Board type is "<< BoardType<<endl; } #pragma once #include "Room.h" class ClassRoom: public Room { private: long capacity; char BoardType; public: ClassRoom(long=0, long=0 , char = ' ', long=0, char = ' '); void ClassRoomExtra(); };
Advertisement
Room (long = 0, long = 0, char = ' ');ClassRoom(long=0, long=0 , char = ' ', long=0, char = ' ');

These dont look right.

When setting default values to parameters, you have to declare a varable:
Room(long numb=0, long fl=0, char build=' ');ClassRoom(long numb=0, long fl=0, char build=' ', long ca=0, char type=' ');


Also, I dont think the following code is going to do what you expect..
Room::Room(long numb, long fl, char build){fl = floor;numb = number;build = building;}

In this code, the paramters are copies of the originala. If you want to
assign a parameter, you need to pass it by refrence (or pointer).

Nontheless, you should not be doing that in a constructor.
Well, to fix your immediate problem, replace

#include "Room.cpp"
#include "ClassRoom.cpp"

with

#include "Room.h"
#include "ClassRoom.h"


More general notes about your code:

- system("PAUSE"); is platform dependent, as well as insecure. PAUSE does not exist in all operating environments, and even in those where it does, it does not necessarily do what you think it does. Besides, you have it placed after a return statement, so it won't be called.

- #pragma once is compiler-specific. Use inclusion guards:
#ifndef HEADER_FILE_NAME_H
#define HEADER_FILE_NAME_H

<your code here>

#endif

- stdafx.h is also compiler specific. It is part of a feature known as "precompiled headers". When you create new projects, select Console Project, then check "Empty Project".


jfl.
Quote:Original post by Crypter
When setting default values to parameters, you have to declare a varable:


Nope, you don't.


Quote:Also, I dont think the following code is going to do what you expect..
Room::Room(long numb, long fl, char build){fl = floor;numb = number;build = building;}

In this code, the paramters are copies of the originala. If you want to
assign a parameter, you need to pass it by refrence (or pointer).

Nontheless, you should not be doing that in a constructor.


Judging from the rest of the code, those are the wrong way around.

Room::Room(long numb, long fl, char build){  floor = fl;  number = numb;  building = build;}
Your problem is that you are including your .cpp files in your main .cpp file, and are thus causing your functions to be defined twice.

The file containing main does need to know about your other classes. However, including these class' implementation files (where their functions are defined) will result in multiple definitions of the same functions, which is not allowed. All you need are the declarations of these functions or classes, so you should include the header files instead (e.g. room.h instead of room.cpp).

Quote:Original post by Crypter
When setting default values to parameters, you have to declare a varable


No, you do not have to name the variables, even if you are using default values. See section 8.3.6 in the standard.
Quote:Original post by Reckoner
...


OP: For more information, see here.
Quote:
No, you do not have to name the variables, even if you are using default values. See section 8.3.6 in the standard.

I knew naming varables in declarations were optional, just didnt know
its valid with default parameters though. Thanks for the info[smile]
thank guys so much I have changed
#include "Room.cpp"
#include #ClassRoom.cpp"
to #inlcude "Room.h" and "ClassRoom.h" and It works!!!! thanks, but i am having another problem, this time the output is not displayed acording to the paramater that i have given in the main file! the integer with value 1 changed to 40000263 something meanwhile the char parameter such as 'M' and 'W' is not even displayed in the console
anyone knows how to sort it out please !!!!!!!!
Quote:Original post by kimtuyenbui
thank guys so much I have changed
#include "Room.cpp"
#include #ClassRoom.cpp"
to #inlcude "Room.h" and "ClassRoom.h" and It works!!!! thanks, but i am having another problem, this time the output is not displayed acording to the paramater that i have given in the main file! the integer with value 1 changed to 40000263 something meanwhile the char parameter such as 'M' and 'W' is not even displayed in the console
anyone knows how to sort it out please !!!!!!!!


Look at Crypter's post and my reply.
thanks guys! I very appreciate that you have spent your time to find out my problem, My program now worked just fine! however i try to convert char to char[20] and once again i have one error that google cant not help me out! can you tell me what i have missed???please

c:\Documents and Settings\Kim Tuyen Bui\Desktop\CW_Visual\Main\ClassRoom.cpp(12): error C2440: '=' : cannot convert from 'char []' to 'char [10]'
c:\Documents and Settings\Kim Tuyen Bui\Desktop\CW_Visual\Main\Room.cpp(10): error C2440: '=' : cannot convert from 'char []' to 'char [20]'

#pragma once
#include <string>
#include "Room.h"


class ClassRoom: public Room
{
private:
int capacity;
char BoardType[10];
public:
ClassRoom(int,int,char[20],int,char[10]);
void ClassRoomExtra();
};

#include <iostream>
#include <string>
#include "stdafx.h"
#include "ClassRoom.h"

using namespace std;

ClassRoom::ClassRoom(int numb = 0, int fl = 0, char build[20] =" ", int ca =0, char type[10] = "")
:Room(numb, fl, build )
{
capacity = ca;
BoardType = type;
}
void ClassRoom::ClassRoomExtra()
{

cout <<" Capacity is "<<" "<<capacity<<" "<<"Board type is"<<" "<< BoardType<<endl;
}

#include <iostream>
#include "stdafx.h"
#include "Room.h"
using namespace std;

Room::Room(int numb, int fl, char build[20])
{
number = numb;
floor = fl;
building = build;
}
void Room::Display()
{
cout << "Room number is " << number<<" At Floor "<<floor<<" In Building "<< building<< endl;
}

#pragma once

class Room
{
protected:
int floor;
int number;
char building[20];
public:
Room (int, int, char[20]);
void Display();
};
#include <iostream>
#include <string>
#include "stdafx.h"
#include "Room.h"
#include "ClassRoom.h"


using namespace std;

int main()
{
ClassRoom A1(2, 3,"Queen", 200, "White");

char command;
cout << "\n******Please Carefully Choose One Of The Following Option*******\n\n"
<<"Press C.......(class room)"
<<"\n......S.......(staff room)"
<<"\n......L.......(lab room)"
<<"\n......M.......(meeting room)"
<<"\n......D.......(meeting room & class room)"
<<"\n......Q....(quit)"
<<"\nEnter Your Command Please -->";
cin >>command;
while (command !='q')
{switch(command)
{
case 'c': A1.Display(),A1.ClassRoomExtra() ;
break;
case 's': cout<<"hi2";
break;
case 'l': cout<<"hi3";
break;
case 'm': cout<<"hi4";
break;
case 'd': cout<<"hi5";
break;
}

cout << "\n******Please Carefully Choose One Of The Following Option*******\n\n"
<<"Press C.......(class room)"
<<"\n......S.......(staff room)"
<<"\n......L.......(lab room)"
<<"\n......M.......(meeting room)"
<<"\n......D.......(meeting room & class room)"
<<"\n......F....(To find a Room)"
<<"\n......Q....(quit)"
<<"\nEnter Your Command Please -->";
cin >>command;
}
return 0;


}

This topic is closed to new replies.

Advertisement