简体   繁体   中英

Class to template error: invalid use of non-static data member

First question here so I hope it's a good one. I have two classes that have several instances of a template inside them. One (grades) is a friend to the other (students), yet when I try to access one of the templates from the other class I get this wonderful g++ error:

error: invalid use of non-static data member 'grades_table::term'

This is the first of a few. I've seen several questions asked in the same vein on here but in looking through them I can't figure out how to apply that information to my problem. Here's all the relevant code.

table_frame.h

class grades_table{

friend class students_table;
public:

grades_table();
    int insert(int& temp_student_ID, std::string& temp_term,
    int& temp_year, char& temp_grade);
    void print(int select_cell = 0);
    void select(std::string& attribute, std::string& identifier);

private:
    int row_number;
    table_column<int> student_ID;
    table_column<std::string> term;
    table_column<int> year;
    table_column<char> grade;
};

tables.cpp

void students_table::print(bool join_id, int select_cell){
    int column_stop;


    column_stop = student_ID.column_depth();
    row_number = 1;

    if(select_cell != 0){
        cout << "(" << student_ID.print(select_cell) << ",";
        cout << first_name.print(select_cell) << ",";
        cout << last_name.print(select_cell) << ")";
    }
    else if(join_id){
        while(row_number <= column_stop){
            //Keep it clean
            if(row_number % 2 == 0){
                cout << "\n";
            }
        cout << "(" << student_ID.print(row_number) << ",";
        cout << first_name.print(row_number) << ",";
        cout << last_name.print(row_number) << ",";
        cout << grades::term.print(row_number) << ","; <<-----ERROR
        cout << grades::year.print(row_number) << ","; <<-----ERROR
        cout << grades::grade.print(row_number) << ")";<<-----ERROR
...

Any help is very much appreciated.

EDIT

Ok, so I changed 'grades_table' to an instance I created called 'grades'. But now it tells me that it hasn't been declared. Here's the main file:

database_control.cpp

#include "table_frame.h"

using namespace std;

void input_output();
void database_actions(const string& command, const string& arguments);
void split(const string &s, char delim, int start, string& argument);

grades_table grades;
students_table students;
bool PROGRAM_EXIT = false;

...

Your compiler is right. You should create instance of grades_table and then only you can access non-static members. I cannot see any static member in your class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM