Problem with storing C++ classes in separate files.

0 Bruce Whealton · October 21, 2014
Hello,
           For some reason, I followed this tutorial exactly like in Video 15 of the C++ video series, but I got an error in the main program.  I put everything inside the same directory/folder.   I did change the class name to MyClass.  So, on the line in main file that reads: MyClass mc; I get an error saying undefined reference to MyClass::MyClass().

Here are my files.
main.cpp:
#include <iostream>
#include "MyClass.h"

using namespace std;


int main()
{

    MyClass mc;
    return 0;
}

MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H


class MyClass
{
    public:
        MyClass();
    protected:
    private:
};

#endif   // MYCLASS_H

And lastly, I have, MyClass.cpp
which is as follows:

#include "MyClass.h"
#include <iostream>


using namespace std;

MyClass::MyClass()
{
    cout << "I am in the MyClass file." << endl;
}

So, why is this not working for me?  Any suggestions?
Thanks,
Bruce
 

Post a Reply

Replies

Oldest  Newest  Rating
0 Bruce Whealton · October 23, 2014
Ok, thanks for the tips.  I have been letting this frustrate me for a while.  Maybe I'll share what I did using Eclipse.  I was watching a tutorial on c and c++ elsewhere and the instructor said that ideally you'd break this code out into separate files with a header and a cpp file to hold the class definition.  He said for simplicity he wasn't going to do that at that point.  So, I tried to do this and I got it wrong.  

So, here is where I started with everything in one file.  It is as follows and it is called class.cpp:
#include <iostream>
using namespace std;

// a very simple class
class Class1 {
int i;
public:
void setValue( int value );
int getValue();
};

void Class1::setValue( int value ) { i = value; }
int Class1::getValue() { return i; }

int main( int argc, char ** argv ) {
int i = 47;
Class1 object1;

object1.setValue(i);
cout << "value is " << object1.getValue() << endl;
return 0;
}

Now, I want to separate that into 3 files, with a header file and a cpp file for the class.  Actually, I got it to work, yea!!!  Now let's see what I did in CodeBlocks that was wrong.  But first, here is what I did in Eclipse to separate that into 3 files.  So, first class.cpp is shorter and is defined as:

#include <iostream>
#include "Class1.h"
using namespace std;

int main( int argc, char ** argv ) {
int i = 47;
Class1 object1;

object1.setValue(i);
cout << "value is " << object1.getValue() << endl;
return 0;
}

Class1.h is as follows:

/*
 * Class1.h
 *  Created on: Oct 22, 2014
 *      Author: Bruce
 */

#ifndef CLASS1_H_
#define CLASS1_H_

class Class1 {
int i;
public:
void setValue( int value );
int getValue();
};

#endif /* CLASS1_H_ */

And Class1.cpp is as follows (a very short file now):
/*
 * Class1.cpp
 *
 *  Created on: Oct 22, 2014
 *      Author: Bruce
 */
// a very simple class
#include "Class1.h"

void Class1::setValue( int value ) { i = value; }
int Class1::getValue() { return i; }

And this works!!!
0 Bruce Whealton · October 23, 2014
My reply wasn't showing up.  I had gotten this to work in my example project in Eclipse.  I was taking another training title on Lynda.com and the author said that ideally we'd want to include a particular class into two separate files, one a header file and another a cpp file.  So, it is a different program but the idea is the same.  By the way, I got this book on CodeBlocks for understanding it along with learning cpp and I still could not get this example program to work in CodeBlocks.  But I did get it to work right in Eclipse.  The errors I get now in CB (CodeBlocks) are very strange and different from previously.  My program is slightly different from the other program in Bucky's New Boston course on video 15 of C++ but I think it is all the same basically.  I mean it is a simple test or example program.  So, here is the program that works in Eclipse.
I have class.cpp as follows:

#include <iostream>
#include "Class1.h"
using namespace std;

int main( int argc, char ** argv ) {
int i = 47;
Class1 object1;

object1.setValue(i);
cout << "value is " << object1.getValue() << endl;
return 0;
}

Then I have Class1.cpp as follows:
/*
 * Class1.cpp
 *
 *  Created on: Oct 22, 2014
 *      Author: Bruce
 */
// a very simple class
#include "Class1.h"

void Class1::setValue( int value ) { i = value; }
int Class1::getValue() { return i; }

and finally, I have Class1.h as follows:

/*
 * Class1.h
 *  Created on: Oct 22, 2014
 *      Author: Bruce
 */

#ifndef CLASS1_H_
#define CLASS1_H_

class Class1 {
int i;
public:
void setValue( int value );
int getValue();
};

#endif /* CLASS1_H_ */

Below is the same program all in one file like the instructor introduced it.

#include <iostream>
using namespace std;

// a very simple class
class Class1 {
int i;
public:
void setValue( int value );
int getValue();
};

void Class1::setValue( int value ) { i = value; }
int Class1::getValue() { return i; }

int main( int argc, char ** argv ) {
int i = 47;
Class1 object1;

object1.setValue(i);
cout << "value is " << object1.getValue() << endl;
return 0;
}

So, I guess for a while, I'll use Eclipse with the cpp course here, unless I can figure out better how things work with codeblocks or what I am doing wrong in codeblocks.
Maybe forum posts are held before being posted as my earlier one didn't appear yet.
Thanks,
Bruce
0 Bruce Whealton · October 21, 2014
I'm confused.  Why did it work for Bucky during the video?  The files are in the same directory.  I did try to find the files and manually add them to the project but that didn't make any difference.  Maybe I will try this within Eclipse and see what the process is.  So, is the problem something I didn't do prior to starting the project or was there an additional step after creating the files in the same directory.  The files are in the same directory together, and the code references them, correctly, so where do I tell it to link to these other files.
Confused,
Bruce
  • 1

C++

126,645 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator