2

I'm currently doing simple class example using Code::Blocks IDE 10.04. On creating new class I encounter undefined reference to myClass::myClass() error. Help me on figuring the error.

myclass.h:

#ifndef MYCLASS_H
#define MYCLASS_H
#include<string>
#include<iostream>
using namespace std;
class myClass
{
    public:
        myClass();
     void showMessage();
        virtual ~myClass();
    protected:
    private:
    string myString;
    int integer;
};

#endif // MYCLASS_H

myclass.cpp:

#include "E:\IOE\VII\Elective-DM\Assignment 2\myClass.h"

myClass::myClass()
{
    //ctor
}

myClass::~myClass()
{
    //dtor
}
void myClass::showMessage()
{
    cout<<"Enter the number ";
    cin>>integer;
    cout<<"Enter the String ";
    cin>>myString;
    cout<<"\nInterger you enter is :-"<<integer<<" and String you enter is "<<myString<<endl;

}

sinpleClass.cpp:

#include<E:\IOE\VII\Elective-DM\Assignment 2\myClass.h>
int main()
{
    myClass myClassObj;
    myClassObj.showMessage();
    return 0;
}
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
Lionel
  • 604
  • 9
  • 26
  • 1
    Please don't use absolute paths to header files... ever. This is most likely also the problem, since the path has "Assignment 2" in it. – StoryTeller - Unslander Monica Jan 23 '13 at 16:27
  • @StoryTeller I used according to link [here](http://www.jusuchyne.com/codingchyne/2011/03/codeblocks-failed-to-find-the-header-file/) – Lionel Jan 23 '13 at 16:29
  • 3
    It's a workaround, not a solution. Furthermore, it was a mistake on their part to even offer that workaround. Don't do it. – StoryTeller - Unslander Monica Jan 23 '13 at 16:30
  • I have not used Codeblocks for years so I do not remember the details on how it builds projects. However is myclass.cpp included in your project's source files? – drescherjm Jan 23 '13 at 16:33
  • 1
    Looks like a source file / project management issue. Is your `myclass.cpp` part of the project? Is it passed to the Linker? – Thomas Matthews Jan 23 '13 at 16:33
  • @ThomasMatthews no... I just tried to add class on snipleClass.cpp and it make header file and cpp file ... I just tried to add new class – Lionel Jan 23 '13 at 16:36
  • @Pattinson, But do you have a project active, or is it just separate files? – chris Jan 23 '13 at 16:37
  • @chris First I create `snipleClass.cpp` file then add class to it... no project active... – Lionel Jan 23 '13 at 16:39
  • @Pattinson, Well, that would be your problem. No project means no linking when you hit build. – chris Jan 23 '13 at 16:39
  • @chris Should we have to create to add class on file or not? – Lionel Jan 23 '13 at 16:41
  • @Pattinson, Unless you do your own linking, you'll need a project any time you use a cpp file without a `main` in it, as is the implementation file in your example. – chris Jan 23 '13 at 16:43

1 Answers1

3

This error is occurs due to the linking error.Later I create new project (according to chris comment above on question) and add class to it the project compile successfully.

Community
  • 1
  • 1
Lionel
  • 604
  • 9
  • 26