i can´t understand this message
this is the central problem
'one or more multiply defined symbols found'
Printable View
i can´t understand this message
this is the central problem
'one or more multiply defined symbols found'
the problem now is:
one or more multiply defined symbols found!
Where did you place the "using namespace std"? Hopefully it is not in the header file Person.h.
Second, your Person.h is wrong. The only thing in there is supposed to be the class definition and any functions that are inline with the class -- nothing else.
That is all that should be in there, nothing else. Then you create a module, Person.cpp that includes this header, and in that file you implement the functions that aren't implemented in the class definition itself.Code:#ifndef PERSON_H
#define PERSON_H
#include <string>
class Person
{
public:
Person();
~Person();
//Accessor Methods
std::string getName () const { return Name; }
std::string getLocation ()const { return Location; }
std::string getAge () const { return Age; }
std::string getOccupation () const { return Occupation; }
void setName (const std::string& Name) { this->Name = Name; }
void setLocation (const std::string& Location) { this->Location = Location; }
void setAge (const std::string& Age) { this->Age = Age; }
void setOccupation (const std::string& Occupation) { this->Occupation = Occupation; }
void createPerson();
void editPerson();
void displayPerson();
void savePerson();
void loadPerson();
private:
std::string Name,
Location,
Age,
Occupation;
};
# endif
Also note that there is no "using namespace std" in the Person.h header file -- this has been discussed many times on this board and other places as to why it is not a good idea to place using clauses in headers. Instead, you see std:: appended to the string type.
Also, what you see in the header are all the headers that are necessary for Person.h to be able to live by itself. That's why <string> is included, because you are using std::string in the class. Your original header file did not #include <string> which was wrong.
Regards,
Paul McKenzie
ok, i´ll do it!
code updated:
Person.H
Cpp fileCode:#ifndef PERSON_H
#define PERSON_H
using namespace std;
class Person
{
public:
//Constructor and Destructor Method:
Person();
~Person();
//funstions:
void createPerson();
void editPerson();
void displayPerson();
void savePerson();
void loadPerson();
//Accessor Methods:
string getName ()const { return Name; }
string getLocation ()const { return Location; }
string getAge ()const{ return Age; }
string getOccupation ()const{ return Occupation; }
void setName (const string& Name) { this->Name = Name; }
void setLocation (const string& Location) { this->Location = Location; }
void setAge (const string& Age) { this->Age = Age; }
void setOccupation (const string& Occupation) { this->Occupation = Occupation; }
private:
//private variables:
string Name,
Location,
Age,
Occupation;
};
# endif
Code:
// Using_class_ in_ c++.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
//scope functions
Person::Person () {cout<<"\n\tBuilding a Person.";}
Person::~Person(){cout<<"\n\tDestroying a Person.";}
void Person::createPerson()
{
Person::editPerson();
}
void Person::editPerson()
{
string TEMP;
system ("CLS");
cout<<"\n\n\t---------------------EDIT person ---------------------------";
cout<<"\n\t Name: ";
cin.ignore();
getline (cin,TEMP);
setName(TEMP);
cout<<"\n\t Location ";
getline (cin,TEMP);
setLocation(TEMP);
cout<<"\n\t Age: ";
getline (cin,TEMP);
setAge(TEMP);
cout<<"\n\t Occupation: ";
getline (cin,TEMP);
setOccupation(TEMP);
}
void Person::displayPerson()
{
system("CLS");
cout<<"\n\n\t---------------------person information----------------------";
cout<< "\n\tName: " , getName();
cout<< "\n\tLocation:" , getLocation();
cout<< "\n\tAge: " , getAge();
cout<< "\n\tOccupation:" , getOccupation();
cout<<"\n\n\t---------------------------------------------------------------\n\n";
}
void Person::savePerson()
{
try
{
ofstream DATAFILE;
DATAFILE.open("Data1.file",ios::out);
DATAFILE <<getName ()<<"\n";
DATAFILE <<getLocation ()<<"\n";
DATAFILE <<getAge ()<<"\n";
DATAFILE <<getOccupation ()<<"\n";
DATAFILE.close(); //Prevents data from getting corrupt incase of sudden shutdown
cout<<"\n\t Success! Data was saved to file.";
}
catch (exception x)
{
cout<<"\n\t File Error! Could not SAVE PERSON.";
}
}
void Person::loadPerson()
{
try
{
string TEMP;
ifstream DATAFILE;
DATAFILE.open("Data1.file",ios::in);
getline (DATAFILE,TEMP);
setName (TEMP);
getline (DATAFILE,TEMP);
setLocation (TEMP);
getline (DATAFILE,TEMP);
setAge (TEMP);
getline (DATAFILE,TEMP);
setOccupation (TEMP);
DATAFILE.close();
}
catch (exception x)
{
cout<<"\n\t File Error! Unable to load data.";
}
}
//end funstions scope
//main method
int main (int argc, char *argv[])
{
Person cg;
char choose[10] = "";
cout<<"\n\t Personnel database 1.0\n";
//system display ("CLS");
//Loop
while (choose [0] != 'q')
{
system ("CLS");
cout<<"\n\t---------------------Main Menu---------------------\n";
cout<<"\n\t| |\n";
cout<<"\n\t| (c) Create person |\n";
cout<<"\n\t| (e) edit information |\n";
cout<<"\n\t| (d) display person |\n";
cout<<"\n\t| (s) Save person |\n";
cout<<"\n\t| (l) Load person |\n";
cout<<"\n\t| (q) quit |\n";
cout<<"\n\t| |\n";
cout<<"\n\t---------------------------------------------------\n\n\t";
cin>>choose;
//system ("CLS");
switch (choose [0])
{
//system ("CLS");
case 'c': cg.createPerson(); break;
case 'e': cg.editPerson(); break;
case 'd': cg.displayPerson(); system ("pause"); break;
case 's': cg.savePerson(); break;
case 'l': cg.loadPerson(); break;
case 'q': cout<<"\n\tExiting main menu..."; break;
default :"\n\tInvalid input!";
}
}
system ("pause");
return 0;
}
no errors on output
but i think the logic is wrong because the program is not saving data, and neither displaying.
:|
You have a problem with displayPerson
This is actually two statements separated by a comma. (The comma operator). First the cout is executed (which shows Name). Then the getName() function is executed with its return value not used. You needCode:cout<< "\n\tName: " , getName();
It is not normal practice to put the class function definitions in the header file. These usually go in a .cpp file. The .h header file normally just contains the class definition. Also it is not advised to put 'using' in a header file. If a header file uses string, cout etc then they are normally referenced via their full scoped name (std::cout, std::string etc). The 'using' statement would go in the .cpp file if required.Code:cout << "\n\tName: " << getName();
if i don´t put namespace in Person.h it not works because the strings types. what the best way to do it?