|
-
May 14th, 2013, 11:31 AM
#31
Re: Unresolved external symbol
i can´t understand this message
this is the central problem
'one or more multiply defined symbols found'
-
May 14th, 2013, 11:34 AM
#32
Re: Unresolved external symbol
the problem now is:
one or more multiply defined symbols found!
-
May 14th, 2013, 11:34 AM
#33
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
i forget to put namespace std, then i fix the error with 'cout´s'. i also should to put stdafx.h in Person.h? or not?
stdafx.h should be the first #include in your .cpp files.
-
May 14th, 2013, 11:36 AM
#34
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
the problem now is:
one or more multiply defined symbols found!
Calm down. You've posted that three times in five minutes. You'll likely get help, but it's not usually going to be instant.
-
May 14th, 2013, 11:38 AM
#35
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
i forget to put namespace std,
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.
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
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.
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
-
May 14th, 2013, 11:41 AM
#36
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
the problem now is:
one or more multiply defined symbols found!
Read my post and fix Person.h. It should not have class functions outside the class in that header. Those functions go into a separate, compilable, C++ module called preferably, Person.cpp.
Regards,
Paul McKenzie
-
May 14th, 2013, 11:55 AM
#37
Re: Unresolved external symbol
-
May 14th, 2013, 12:07 PM
#38
Re: Unresolved external symbol
code updated:
Person.H
Code:
#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
Cpp file
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;
}
-
May 14th, 2013, 12:09 PM
#39
Re: Unresolved external symbol
no errors on output
but i think the logic is wrong because the program is not saving data, and neither displaying.
:|
-
May 14th, 2013, 12:12 PM
#40
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
code updated:
Person.H
Code:
#ifndef PERSON_H
#define PERSON_H
using namespace std;
class Person
{
...
};
# endif
Did't you read what Paul wrote?
 Originally Posted by Paul McKenzie
Where did you place the "using namespace std"? Hopefully it is not in the header file Person.h.
Victor Nijegorodov
-
May 14th, 2013, 12:13 PM
#41
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
no errors on output
but i think the logic is wrong because the program is not saving data, and neither displaying.
So why didn't you debug it to see what, where and why goes wrong?
Victor Nijegorodov
-
May 14th, 2013, 12:26 PM
#42
Re: Unresolved external symbol
You have a problem with displayPerson
Code:
cout<< "\n\tName: " , getName();
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 need
Code:
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 14th, 2013, 12:31 PM
#43
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
no errors on output but i think the logic is wrong because the program is not saving data, and neither displaying.
See my post #42 re displaying. My copy of the program saves and loads data OK. 'type data1.file' at the command prompt to show the current data.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 14th, 2013, 12:34 PM
#44
Re: Unresolved external symbol
if i don´t put namespace in Person.h it not works because the strings types. what the best way to do it?
-
May 14th, 2013, 12:37 PM
#45
Re: Unresolved external symbol
 Originally Posted by crisdeveloper
if i don´t put namespace in Person.h it not works because the strings types. what the best way to do it?
See last para of my post #42. In the header file you use std::string, std::cout etc etc etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|