Help with Classes/Objects please
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help with Classes/Objects please

  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Help with Classes/Objects please

    Hi, I am completly new to Classes in C++. I have been trying to create a simple class and have had all sorts of problems. I am following directions from my textbook and the code looks correct, I have tried searching online for answers to the compiler errors but nothing was helpfull.

    driverProg
    Code:
    
    #include "MyObject.h"
    #include <iostream>
    
    using namespace std;
    
    
    
    int main()
    {
    
        int newAge=23;
        char newTitle= 'G';
    
        /* Trying to initialize an object */
        MyObject thing;
    
        /* Trying to use object methods */
        thing.setAge(newAge);
        thing.setTitle(newTitle);
    
        /* Trying to access object variables */
        cout<<"Age : "<< thing.getAge() <<endl;
        cout<<"Title : "<< thing.getTitle() <<endl;
    
        return(0);
    }
    
    -------------------------
    Compiler Log:
    
     undefined reference to `MyObject::setAge(int)'
     undefined reference to `MyObject::setTitle(char)'
     undefined reference to `MyObject::getAge() const'
     undefined reference to `MyObject::getTitle() const'
    I think the above errors are because I am unable to compile the MyObject.cpp file.


    MyObject.cpp
    Code:
    #include "MyObject.h"
    #include <iostream>
    using namespace std;
    
    /* Sets the age of the Object */
    void MyObject::setAge(int num)
    {
        if(num<0)
            age=1;
        else
            age=num;
    }
    
    /* Sets the title of the Object */
    void MyObject::setTitle(char ch)
    {
        title=ch;
    }
    
    /* Returns the value in member var "age" */
    int MyObject::getAge() const
    {
        return(age);
    }
    
    /* Returns the value in the member var. "title" */
    char MyObject::getTitle() const
    {
        return(title);
    }
    
    
    --------------------------------
    Compiler Log:
    
    Linking console executable: ...
    F:/CodeBlocks/....
     undefined reference to `_WinMain@16'
    collect2: ld returned 1 exit status
    I am not sure what this WinMain@16 error is all about.

    MyObject.h
    Code:
    #ifndef MYOBJECT_H
    #define MYOBJECT_H
    
    class MyObject
    {
        private:
            int age;
            char title;
    
        public:
            void setAge(int);
            void setTitle(char);
    
            int getAge() const;
            char getTitle() const;
    };
    
    #endif
    
    --------------------------------------
    Compiler Log:
    
    Nothing to be done.

    I have all 3 files in the same directory and I have no idea why the MyObject.cpp will not compile. I tried creating them as a project which resulted in similar errors. Any suggestions or anyone see what I am doing wrong? I am using Code::Blocks IDE and GCC compiler.
    Google is your friend.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: Help with Classes/Objects please

    Quote Originally Posted by g.eckert
    I am not sure what this WinMain@16 error is all about.
    You have no global main function in that source file, but tried to compile and link it into an executable program. You should have just compiled the source file into an object file, and then link the object file together with the object file generated from the other source file containing your global main function.

    Quote Originally Posted by g.eckert
    I tried creating them as a project which resulted in similar errors.
    That should be the solution: put the files in a project and then build the project.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: Help with Classes/Objects please

    I continued to try the Project method and I eventually got it to work. I was previously still compiling the files individualy within the project. Then i got it to work by building the entire project.

    You should have just compiled the source file into an object file, and then link the object file together...
    I am going to look into doing this since I have no idea what the Project is actually doing. I am guessing it is the same thing but If i am able to do it on my own I would be a little more confident about using classes and how the driver/object file interact.
    Google is your friend.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: Help with Classes/Objects please

    Quote Originally Posted by g.eckert
    I am going to look into doing this since I have no idea what the Project is actually doing. I am guessing it is the same thing but If i am able to do it on my own I would be a little more confident about using classes and how the driver/object file interact.
    If you are going to do this on your own, then you need to learn how to use the compiler at the command line. Refer to the GCC online documentation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Help with Classes/Objects please

    You can try code block to create the project in *Nix system.
    Thanks for your help.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: Help with Classes/Objects please

    Quote Originally Posted by Peter_APIIT
    You can try code block to create the project in *Nix system.
    Not much point; it would be roughly the same as on Windows.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center