lundi 7 mai 2018

C++ has no member named '...'

I know this is very basic but I don't understand what I am doing wrong but I am getting the following error:

user@host$ g++ -std=c++11 Test.h Test.cpp Play.cpp -o main
Test.cpp:13:18: error: no ‘int Test::getA() const’ member function declared in class ‘Test’
 int Test::getA() const {
                  ^
Play.cpp: In function ‘int main()’:
Play.cpp:11:15: error: ‘class Test’ has no member named ‘getA’
     cout << a.getA() << endl;
               ^
user@host$

I have three files:

  • Test.h : header file for the class Test.
  • Test.cpp : implementation file for class Test.
  • Play.cpp : main() file that calls/tests the header/implementation.

Test.h:

#ifndef TEST_H
#define TEST_H
using namespace std;

class Test {
    public:
    Test ();
    Test (int a);
    int getA() const;
    void hello();
    int a;
};
#endif

Test.cpp:

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

Test::Test() {
    a = 3;
}

Test::Test(int a) {
    this->a = a;
}

int Test::getA() const {
    return a;
}

void Test::hello() {
    a = 4;
    cout << a << endl;
}

Play.cpp:

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


int main() {

    Test a(2);
    a.hello();

    cout << a.getA() << endl;

    return 0;
}

I have no idea why this is not working. I've tried similar programs that are already completed but I can't tell the difference. This can't be an issue with my compiler (gcc or g++)?

Aucun commentaire:

Enregistrer un commentaire