Compile error that doesn't point to right line?

[I read the sticky and I'm trying to give useful information so someone can help me but I'm a little frustrated so if I'm overlooking something tell me]

I've got a really odd compile error and some more typical ones:

main.cpp: In constructor `cup::cup()':
main.cpp:9: error: `int container::cap' is private
main.cpp:23: error: within this context
main.cpp:9: error: `int container::cap' is private
main.cpp:23: error: within this context
main.cpp:10: error: `int container::full' is private
main.cpp:23: error: within this context
main.cpp:10: error: `int container::full' is private
main.cpp:23: error: within this context

main.cpp: At global scope:
main.cpp:27: error: new types may not be defined in a return type
main.cpp:27: error: two or more data types in declaration of `main'
main.cpp:27: error: extraneous `int' ignored
main.cpp:27: error: `main' must return `int'

make.exe: *** [main.o] Error 1

For this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class container
{
      int cap;
      int full;
      string type;
 protected:
      container()
      {cap = 0; full = 0;}
      container(int a, int b)
      {cap = a; full = b;}
}
    
class cup : public container
{
 public:
        cup()
        {cap = 50; full = 0;}
}

int main()
{
    cup();
    cout << "hello.";
    return 0;
}


I'm running Dev-C++ on XP if that helps.

I don't see what the problem is with line 27, though I did find a way to prevent the error. I removed the cup class and it successfully compiled, but I can't figure out for the life of me how the errors for line 27 are related to the class and what's wrong with it. Also, I expected the cup class to inherit the cap and full variables from the container class and be able to access them from the cup() constructor without a problem whether they're private or not because they're in the same class. Umm, any help? They're probably glaringly obvious mistakes but I'm pretty new to programming.
Last edited on
Classes have to have a semicolon after the ending braces.
Last edited on
Damn, the infamous missing-semicolon strikes again >:|

Still don't know why I'm getting access errors though.
Last edited on
You can't access private members inside constructors I believe. I would suggest creating functions to modify them.
Oh... Thanks! Seems kind of counter-intuitive though.

EDIT: Hrm, works when they're protected too. Is it bad practice to have fields that are protected rather than private? Should I use a method to set the values or just set the fields to protected?
Last edited on
Yeah...I think it's because derived classes can use the constructors you specify in the base class, which might or might not be able to access the private variables (not sure though)
[I made an edit above]

I recall that constructors in derived classes will call the default constructor in their base class (unless you specify another one), but I thought functions should be able to access a variable if it's in the same class as the function.
They can, but constructors are special in the fact that derived classes could use them, and that derived classes could possibly not have access the the private values of the base class.

And no, I don't think it is...although I'm not really an expert on good/bad coding practices XD
When a class D inherits from a class B, D can access all of B's public and protected members.

To clarify, given:

1
2
3
4
5
6
7
8
class B {
  public:
      int x;
  protected:
      int y;
  private:
      int z;
};


Anyone at all can access x. Only B and any class derived from B can access y. Only B can access z (not even derived classes).
Topic archived. No new replies allowed.