NoName NoName - 1 month ago 17
C++ Question

C++ : Member of class not being recognised

I am fairly new to c++ (java background) and I'm trying to access a member of a class I have created but I keep getting an error message when trying to call a member of a class, it's saying that the variable is not a member of the class.

Any ideas why this is happening? I've looked at so many other examples of people with this problem, but none of them have helped me find out why

Main.cpp

#include "stdafx.h"
#include "Adressbuch.h"
#include "Kontakt.h"
#include <iostream>
#include <sstream>

using namespace std;

Adressbuch hinzufügen(Adressbuch buch);
Adressbuch löschen(Adressbuch buch);
void auflisten(Adressbuch buch);

int main()
{
bool end = true;
Adressbuch buch;
while (end) {
cout << "Bitte geben sie ein Aktion ein: (hinzufügen(h)/löschen(l)/beenden(b)/auflisten(a))"
<< endl << "zur Zeit gibt es " << buch.adress_buch.size() << " Kontakte" << endl;
if (cin >> "h") buch = hinzufügen(buch);
else if (cin >> "l") buch = löschen(buch);
else if (cin >> "a") auflisten(buch);
else if (cin >> "b") end = true;
else cout << "Error. Ungultig Eingabe." << endl;
}
return 0;


Adressbuch.h

#include "Kontakt.h"
#include <list>

class Adressbuch{
public:
Adressbuch();
~Adressbuch();
void hinzufügen(Kontakt k);
void löschen(Kontakt k);

list<Kontakt> Adressbuch::adress_buch;
};


Adressbuch.cpp

#include "Adressbuch.h"
#include "Kontakt.h"
#include <list>
using namespace std;

Adressbuch::Adressbuch(){
adress_buch;
}

Adressbuch::~Adressbuch(){
}

void Adressbuch::hinzufügen(Kontakt k){
adress_buch.push_back(k);
}

void Adressbuch::löschen(Kontakt k) {
adress_buch.remove(k);
}


The member that I am having trouble with, is the list adress_buch. Anytime I try to call it, it says its not a member, even though it is defined in the header class?

Error message on line 19 of main()

Severity Code Description Project File Line Suppression State
Error C2039 'adress_buch': is not a member of 'Adressbuch'
ConsoleApplication5 c:\users\gregs\documents\visual studio
2015\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp 19

Answer

First a minimal, complete verifiable example that contains nothing but the code required to trigger the error:

#include <list>
using namespace std;

class Adressbuch
{
public:
    list<int> Adressbuch::adress_buch;
};
int main()
{
    Adressbuch buch;

    buch.adress_buch.size();
    return 0;
}

That's all that's needed, little bit more than, to find the problem. With nothing else in the way as a distraction problem 1 is easy to spot. I'm no guru in the Visual Studio compiler and I don't have one available, but I'm betting that somewhere in the warnings or errors is this line:

list<int> Adressbuch::adress_buch;

adress_buch is improperly defined causing all sorts of future problems. Compiling this example, GCC gives:

error: extra qualification 'Adressbuch::' on member 'adress_buch'

A corrected example is

#include <list>
using namespace std;

class Adressbuch
{
public:
    list<int> adress_buch;
};
int main()
{
    Adressbuch buch;

    buch.adress_buch.size();
    return 0;
}

Or better

#include <list>

class Adressbuch
{
public:
    std::list<int> adress_buch;
};
int main()
{
    Adressbuch buch;

    buch.adress_buch.size();
    return 0;
}

Because the notorious, bug-hiding using namespace std; has been removed