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.