CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 59
  1. #1
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    [RESOLVED] Unresolved external symbol

    perhaps this can be a very popular error, but i´m beginner and i not used to see problems like this

    Code:
    #include <stdio.h>
    #include <tchar.h>
    
    #include <iostream>
    #include <string>
    #include <fstream>// enable writing to and reading from files
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    
    class Person
    {    
    
    public:
    
       //Constructor and Destructor Methods
    
       Person(){cout<<"\n\tBuilding a Person.";};
       ~Person(){cout<<"\n\tDestroying a Person.";};
    
       //Accessor Methods
    
       string getName       (){ return Name; }
       string getLocation   (){ return Location; }
       string getAge        (){ return Age; }
       string getOccupation (){ return Occupation; }
    
       void setName       (string Name)       { Name = Name; }
       void setLocation   (string Location)   { Location = Location; }
       void setAge        (string Age)        { Age = Age; }
       void setOccupation (string Occupation) { Occupation = Occupation; }
    private:
    
       string Name,
             Location,
             Age,
             Occupation;
    
    
    };
    
    
    
    //Function prototypes
    
    void createperson();
    void editperson();
    void displayperson();
    void saveperson();
    void loadperson();
    
    
    
    int main (int argc, char *argv[])
    {
       
       Person* cg = new Person();
    
       char choose[10] = "";
       
        cout<<"\n\t Personnel database 1.0\n";
    
        //system display ("CLS");
    
        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': createperson(); break;
            case 'e': editperson(); break;
            case 'd': displayperson(); system ("pause"); break;
            case 's': saveperson(); break;
            case 'l': loadperson(); break;
            case 'q': cout<<"\n\tExiting main menu..."; break;
            default :"\n\tInvalid input!";
            }
          
        }
    
         system ("pause");
          return 0;
    
    }
    
    //Function´s scope
    
    
    void createperson()
    {
    
       Person* cg = new Person();
       
    }
    
    void editPerson()
    {
        Person* cg = new Person();
       
       string TEMP;
        system ("CLS");
        cout<<"\n\n\t---------------------EDIT person ---------------------------";
    
        cout<<"\n\t Name: ";
        getline (cin,TEMP);
        cg->setName(TEMP);
    
        cout<<"\n\t Location ";
        getline (cin,TEMP);
        cg->setLocation(TEMP);
    
        cout<<"\n\t Age: ";
        getline (cin,TEMP);
        cg->setAge(TEMP);
    
        cout<<"\n\t Occupation: ";
        getline (cin,TEMP);
        cg->setOccupation(TEMP);
    }
    
    void displayPerson()
    {
       Person* cg = new Person();
    
        system("CLS");
        cout<<"\n\n\t---------------------person information----------------------";
        cout<< "\n\tName: "      , cg->       getName();
        cout<< "\n\tOccupation:" , cg->   getLocation();
        cout<< "\n\tLocation: "  , cg->        getAge();
        cout<< "\n\tReferences:" , cg-> getOccupation();
        cout<<"\n\n\t---------------------------------------------------------------\n\n";
    }
    
    void savePerson()
    {
       Person* cg = new Person();
    
    
        try
        {
            ofstream DATAFILE;
            DATAFILE.open("Data1.file",ios::out);
    
            DATAFILE <<cg->getName       ()<<"\n";
            DATAFILE <<cg->getLocation ()<<"\n";
            DATAFILE <<cg->getAge   ()<<"\n";
            DATAFILE <<cg->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 loadperson()
    {
       Person* cg = new Person();
    
        try
        {
            string TEMP;
            ifstream DATAFILE;
            DATAFILE.open("Data1.file",ios::in);
            getline (DATAFILE,TEMP);
    
            cg->setName (TEMP);
            getline (DATAFILE,TEMP);
    
            cg->setLocation (TEMP);
            getline (DATAFILE,TEMP);
    
            cg->setAge (TEMP);
            getline (DATAFILE,TEMP);
    
            cg->setOccupation (TEMP);
            DATAFILE.close();
        }
        catch (exception x)
        {
            cout<<"\n\t File Error! Unable to load data.";
        }
    }
    erros list:

    Code:
    Error   1   error LNK2019: unresolved external symbol "void __cdecl saveperson(void)" (?saveperson@@YAXXZ) referenced in function _main   H:\Cry_Dev\Programming\C++\Using_class_ in_ c++\Using_class_ in_ c++\Using_class_ in_ c++.obj
    Error   2   error LNK2019: unresolved external symbol "void __cdecl displayperson(void)" (?displayperson@@YAXXZ) referenced in function _main   H:\Cry_Dev\Programming\C++\Using_class_ in_ c++\Using_class_ in_ c++\Using_class_ in_ c++.obj
    Error   3   error LNK2019: unresolved external symbol "void __cdecl editperson(void)" (?editperson@@YAXXZ) referenced in function _main   H:\Cry_Dev\Programming\C++\Using_class_ in_ c++\Using_class_ in_ c++\Using_class_ in_ c++.obj
    Error   4   error LNK1120: 3 unresolved externals   H:\Cry_Dev\Programming\C++\Using_class_ in_ c++\Debug\Using_class_ in_ c++.exe   1
    what i can do to fit this problem?
    Last edited by crisdeveloper; May 13th, 2013 at 02:13 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Unresolved external symbol

    c++ is case sensitive. ie displayperson is different to displayPerson.

    Code:
    void editperson();
    void displayperson();
    void saveperson();
    
    ...
    
    void displayPerson()
    {
    You need to check that the case of your function declarations are the same as those of your function definitions and that they are also the same where the function is used.
    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)

  3. #3
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    good observation!!! i ´ll take a look! thanks!

  4. #4
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    the program "works well", but it no saves , no displays and no load the information that i type.

    Code:
    // Using_class_ in_ c++.cpp : Defines the entry point for the console application.
    //
    
    #include <stdio.h>
    #include <tchar.h>
    
    #include <iostream>
    #include <string>
    #include <fstream>// enable writing to and reading from files
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
    
    
    class Person
    {    
    
    public:
    
    	//Constructor and Destructor Methods
    
    	Person(){cout<<"\n\tBuilding a Person.";};
       ~Person(){cout<<"\n\tDestroying a Person.";};
    
    	//Accessor Methods
    
    	string getName       (){ return Name; }
    	string getLocation   (){ return Location; }
    	string getAge        (){ return Age; }
    	string getOccupation (){ return Occupation; }
    
    	void setName       (string Name)       { Name = Name; }
    	void setLocation   (string Location)   { Location = Location; }
    	void setAge        (string Age)        { Age = Age; }
    	void setOccupation (string Occupation) { Occupation = Occupation; }
    private:
    
    	string Name,
    		   Location,
    		   Age,
    		   Occupation;
    
    
    };
    
    
    
    //Function prototypes
    
    void createperson();
    void editperson();
    void displayperson();
    void saveperson();
    void loadperson();
    
    
    
    int main (int argc, char *argv[])
    {
    	
    	Person* cg = new Person();
    
    	char choose[10] = "";
       
        cout<<"\n\t Personnel database 1.0\n";
    
        //system display ("CLS");
    
        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': createperson(); break;
            case 'e': editperson(); break;
            case 'd': displayperson(); system ("pause"); break;
            case 's': saveperson(); break;
            case 'l': loadperson(); break;
            case 'q': cout<<"\n\tExiting main menu..."; break;
            default :"\n\tInvalid input!";
            }
          
        }
    
    	  system ("pause");
          return 0;
    
    }
    
    //Function´s scope
    
    
    void createperson()
    {
    
    	Person* cg = new Person();
    	
    }
    
    void editperson()
    {
        Person* cg = new Person();
    	
    	string TEMP;
        system ("CLS");
        cout<<"\n\n\t---------------------EDIT person ---------------------------";
    
        cout<<"\n\t Name: ";
    	cin.ignore();
        getline (cin,TEMP);
        cg->setName(TEMP);
    
        cout<<"\n\t Location ";
        getline (cin,TEMP);
        cg->setLocation(TEMP);
    
        cout<<"\n\t Age: ";
        getline (cin,TEMP);
        cg->setAge(TEMP);
    
        cout<<"\n\t Occupation: ";
        getline (cin,TEMP);
        cg->setOccupation(TEMP);
    }
    
    void displayperson()
    {
    	Person* cg = new Person();
    
        system("CLS");
        cout<<"\n\n\t---------------------person information----------------------";
        cout<< "\n\tName: "      , cg->       getName();
        cout<< "\n\tOccupation:" , cg->   getLocation();
        cout<< "\n\tLocation: "  , cg->        getAge();
        cout<< "\n\tReferences:" , cg-> getOccupation();
        cout<<"\n\n\t---------------------------------------------------------------\n\n";
    }
    
    void saveperson()
    {
    	Person* cg = new Person();
    
    
        try
        {
            ofstream DATAFILE;
            DATAFILE.open("Data1.file",ios::out);
    
            DATAFILE <<cg->getName       ()<<"\n";
            DATAFILE <<cg->getLocation ()<<"\n";
            DATAFILE <<cg->getAge   ()<<"\n";
            DATAFILE <<cg->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 loadperson()
    {
    	Person* cg = new Person();
    
        try
        {
            string TEMP;
            ifstream DATAFILE;
            DATAFILE.open("Data1.file",ios::in);
            getline (DATAFILE,TEMP);
    
            cg->setName (TEMP);
            getline (DATAFILE,TEMP);
    
            cg->setLocation (TEMP);
            getline (DATAFILE,TEMP);
    
            cg->setAge (TEMP);
            getline (DATAFILE,TEMP);
    
            cg->setOccupation (TEMP);
            DATAFILE.close();
        }
        catch (exception x)
        {
            cout<<"\n\t File Error! Unable to load data.";
        }
    }

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Unresolved external symbol

    the program "works well", but it no saves , no displays and no load the information that i type.
    Well it now compiles and links, but I would hardly claim that it 'works well' when it doesn't load, save or display the information!

    What debugging of the program have you done? Have you traced through the program using the debugger to see where your program deviates from that which you expected?

    Hint. The scope of variables defined within a function are local to that function. So if you define the same variable (say *cg) in several functions, each of these definitions refer to a different 'cg' even though they have the same name.

    Also, you have memory leaks as you are using 'new' but are not deleting the memory allocated anywhere.
    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)

  6. #6
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    if i retire the objects from the functions i have some problems like "cg is undefined"
    how i can do to make a global object for all the functions?

  7. #7
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    ok, i´ve made a global object before main method
    but nothing changed

  8. #8
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    i my directory was created a file Data1.file how can i read it to check if the data is saved?

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Unresolved external symbol

    Quote Originally Posted by crisdeveloper View Post
    i my directory was created a file Data1.file how can i read it to check if the data is saved?
    Notepad? The VC editor?

  10. #10
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    not. these txt editors only show me a empty txt.

  11. #11
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    i meam that the program compiles well but not work like i would want.

    i tried to understand what wrong is happening, but still i don´t know waht i can do. i already try to put the function save inside the function editPerson, but no progress in it.

  12. #12
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    i´m instaling fileViewPro to open data1.file.

  13. #13
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Unresolved external symbol

    See 2kaud's hint in post #5
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Unresolved external symbol

    Basically, you have a couple of choices. You can either make cg a global variable, or define it in main and pass it as a parameter to each of the used functions.

    If you define it at global scope then its definition would be after the definition of the class, but before main. Then in all the functions, the definition for cg would be removed.

    Note that you also have a problem with your class

    Code:
    void setName       (string Name)       { Name = Name; }
    This doesn't do what you think it does. You need either to have

    Code:
    void setName       (string Name1)       { Name = Name1; }
    or

    Code:
    void setName       (string Name)       { this->Name = Name; }
    It's usually not a good idea to have the name of parameters the same as class members.

    You also need to delete the memory allocated in the createperson function or you have a memory leak.
    Last edited by 2kaud; May 13th, 2013 at 04:20 PM.
    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)

  15. #15
    Join Date
    May 2013
    Location
    // Brazil
    Posts
    51

    Re: Unresolved external symbol

    fileview pro is not for open this kind of file.
    but the question is not this. the most imoprtant is make the program works welll!!

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured