Protected members "not declared in this scope" in derived classes

So, I'm working with inheritance for the first time. The concept and implementation seemed pretty straight forward after reviewing the tutorial on this site. However, my derived class seems to be unable to see the protected members of the parent class. It's probably some syntactical nuance I don't see. The offending classes and, in the event its relevant, the function calls from main() are below

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
33
34
35
36
37
38
39
40
41
class SteamTable
{
    public:
        SteamTable();
        virtual ~SteamTable();
        static const size_t minCols = 3; // Minimum number of columns in the table

        virtual void generateTable(helmholtz::num_t low, helmholtz::num_t high, helmholtz::num_t pInterval);
        virtual void printTable(std::ostream& os);
        virtual void precision(int p);
        virtual int  precision();

    protected:
        virtual void verifyRange();
        State* state;  // Pointer to a State object to be used for calculations
        std::ostream os; // Ostream object to which the table will be output.
        std::vector<std::string> outStrings; // Container of the strings to be output
        std::vector<size_t> colWidth; // Width of each column
        size_t numCols; // Number of columns in the Table
        unsigned int nTable;     // Table type: 1 == Tsat, 2 == Psat, 3 == Superheated steam, 4 == Subcooled & superheated simple grid
        int prec;       // Precision of the output
        helmholtz::num_t low, high, pInterval; // Lowest & highest limits and interval of independent property


    private:
};

class STable1 : public SteamTable
{
    public:
        STable1();
        virtual ~STable1();
        virtual void generateTable(helmholtz::num_t low, helmholtz::num_t high, helmholtz::num_t pInterval);
        virtual void printTable(std::ostream& os);

        static const unsigned int nTable = 1;

    protected:

    private:
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    SteamTable* table = new STable1;
    table->precision(5);
    table->generateTable(0.015, 0.1, 0.05);
    table->printTable(std::cout);

    std::ofstream myfile;
    myfile.open ("example.txt");
    table->printTable(myfile);
    myfile.close();

    delete table;

    return 0;
}
I don't see anywhere in the code you've posted where a derived class is trying to access a protected member of its base class.
Last edited on
To reduce size of the initial post, I didn't include the functions generating the result, as I believed it was likely some problem with the declaration. As I went to copy the function, I discovered the problem: I didn't have the Class identifier (STable1::) on my function names in my implementation. I've cleaned that up and corrected the error, but now I've got a completely (I think) unrelated error that I have no idea how to approach. Using the class declarations above:

1
2
3
4
5
SteamTable::SteamTable()
{
    //ctor
    state = new State;
}


error:[1] ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]’ is protected within this context
You are posting bits and pieces that aren't actually causing problems. You use an ostream& in printTable(). Why not post that code?

Does the error point to this constructor? You suggest that it does, but you don't really give us much context. I suspect that printTable() tries to create a SteamTable object of some sort and the constructor of the base class happens to be the last thing processed before the error is encountered.

By the way, line 16 in the first post won't work. std::ostream cannot be created without a streambuf* argument.

Edit: wait, I don't know what your constructor does, so it might be OK. However, I doubt you need this member because you pass a std::ostream& in printTable().
Last edited on
Not syntactic, but semantic.

std::ostream can't be default-constructed, nor can it be copied. Its default constructor is both protected and deleted.
Last edited on
Yeah, I realized after doug4's original post that I wouldn't need that member anyway because I'm passing it in by reference. I'm encountering other issues now, but it's completely unrelated to the original problem or the second one.

Thanks, all!
Topic archived. No new replies allowed.