-3

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  
NoName
  • 49
  • 4
  • 1
    You need to `#include "Kontakt.h"` in the header. – πάντα ῥεῖ Nov 06 '16 at 21:29
  • Sorry, I did in my actual code, just forgot to put it in the main code part here – NoName Nov 06 '16 at 21:30
  • And you need to learn some basic C++. – juanchopanza Nov 06 '16 at 21:31
  • the calling of functions isnt causing any errors, its just that member adress_buch, thats the only one giving me errors – NoName Nov 06 '16 at 21:36
  • 1
    Out of curiosity, what is `adress_buch;` doing camping out in your constructor ? And with `list Adressbuch::adress_buch;`, the `Adressbuch::` is not required. – WhozCraig Nov 06 '16 at 21:41
  • @WhozCraig Nah. OP got me with that, too. There are free functions with the same name as the methods inflating the confusion. – user4581301 Nov 06 '16 at 21:43
  • @user4581301 yeah, I see that now. I still don't see the *point*, however. but granted, you're (they) are correct. – WhozCraig Nov 06 '16 at 21:44
  • the hinzufugen(buch) / loschen(buch) in the main is a function of the main which then invokes the function from the Adressbuch class, I havent included it here because its irrelevant to my problem, sorry I know having similar named functions with some omitted can be confusing – NoName Nov 06 '16 at 21:44
  • Good time to read up on the [mcve], NoName. Not only does it present a clearer picture for us out here, but odds are really good that forming one will expose your problem and suggest the solution. – user4581301 Nov 06 '16 at 21:47
  • Also, that `list` should be `std::list` in that class member list. I see no `using namespace std;` ([which is actually a *good* thing](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) until after your include list in your main source file. The *exact* error message, verbatim, and the line marked in your *real* source list that is exhibiting that error is warranted anytime an error is questioned on SO, and should be part of the question. – WhozCraig Nov 06 '16 at 21:49
  • Thanks, I'll read up on giving better examples in my questions, I know having similar function names can be confusing, but do you have any idea as to why the member isn't being recognised when I try to call it in my main e.g `buch.adress_buch.size()` is giving me this error – NoName Nov 06 '16 at 21:50
  • `list Adressbuch::adress_buch;` is a completely broken class member declaration. It should be `std::list adress_buch;`, after [getting rid of the "using namespace std;" kludge](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). You need to completely forget that "using namespace std;" is a part of the C++ language. – Sam Varshavchik Nov 06 '16 at 21:57
  • Thanks I added the `std::` cast and it fixed the error, so in future rathe than using namespace std, should I just cast all my global variables with `std::`? – NoName Nov 06 '16 at 22:00

1 Answers1

0

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

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • thanks for the example, will use this practice in the future, also adding the std:: cast seemed to fix the error for me – NoName Nov 06 '16 at 22:07