0

I just recently started messing around with separate class files in c++ and this was my first attempt:

First I made a class header called "ThisClass.h":

//ThisClass.h

#ifndef THISCLASS_H
#define THISCLASS_H

class ThisClass
{
private:
    int x;
    float y;

public:
    ThisClass(int x, float y);
    void setValues(int x, float y);
    int printX();
    float printY();
};
#endif // THISCLASS_H

Then, I implemented my class in a file called "ThisClass.cpp":

//ThisClass.cpp

#include "ThisClass.h"

ThisClass::ThisClass(int x, float y)
{
    this->x = x;
    this->y = y;
}

void ThisClass::setValues(int x, float y)
{
    this->x = x;
    this->y = y;
}

int ThisClass::printX()
{
    return this->x;
}
float ThisClass::printY()
{
    return this->y;
}

Finally, I made a file called "main.cpp" where I used the class:

//main.cpp

#include <iostream>

    using namespace std;

    int main()
    {
        ThisClass thing(3, 5.5);
        cout << thing.printX() << " " << thing.printY()<< endl;
        thing.setValues(5,3.3);
        cout << thing.printX() << " " << thing.printY()<< endl;
        return 0;
    }

I then compiled and ran this program through Code Blocks which uses the MinGW compiler and received the following errors:

In function 'int main()':|
main.cpp|7|error: 'ThisClass' was not declared in this scope|
main.cpp|7|error: expected ';' before 'thing'|
main.cpp|8|error: 'thing' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Am I somehow doing this wrong? Any help would be appreciated.

Orren Ravid
  • 560
  • 1
  • 6
  • 24

2 Answers2

2

You forgot to #include "ThisClass.h" in main.cpp.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

As already answered that you forgot to put #include "ThisClass.h"in main.cpp.

Just do that and your code will be compiled. I just want to answer your question - However, now my console isn't outputting anything even though I have 2 cout calls Please put a getchar() before return in main function, it will allow you to see your output.

gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
  • Thanks for the reply, however the real reason the console wasn't working was because i added the flag -mwindows which adds libraries that disable to console. – Orren Ravid Jul 20 '16 at 19:59