0

I am trying to make a program that takes in the ID and marks of each student with user input using template class. But when I try to create the student array, these errors occured.

darkxith@DESKTOP-2UFE545:~/proA2$ g++ test.h test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:80:59: error: expression list treated as compound expression in initializer [-fpermissive]
         student<string, int> student[i](id, eng, math, jan);
                                                           ^
test.cpp:80:59: error: array must be initialized with a brace-enclosed initializer [-fpermissive]
test.cpp:80:59: error: conversion from ‘int’ to non-scalar type ‘student<std::__cxx11::basic_string<char>, int>’ requested
darkxith@DESKTOP-2UFE545:~/proA2$ 

This is my header file:

#include <string>
#include <iostream>
using namespace std;

template<class T, class U>
class student
{
    private:
        T id;
        U eng, math, jan, total;
    public:
        student (T id, U eng, U math, U jan)
        {
            id = id;
            eng = eng;
            math = math;
            jan = jan;
            total = eng + math + jan;
        }
};

This is my cpp file:

#include <string>
#include <iostream>
#include <exception>
#include "test.h"
using namespace std;
#define MAX 10

int main()
{
    string id;
    int eng, math, jan, i;
    for ( i = 0; i < MAX; i++)
    {
        cout << i+1 << "番目の学生データを入力してください。" << endl;
        cout << "IDを入力してください:";
        cin >> id;

        cout << "英語の点数:";
        cin >> eng;
        
        cout << "数学の点数:";
        cin >> math;

        cout << "国語の点数:";
        cin >> jan;

        student<string, int> student[i](id, eng, math, jan);
    }
}

If the student class wasn't declared array, it works fine but I need to input multiple student data so I am currently stuck in this step.

3
  • 1
    Consult your C++ programming text's section on arrays. Commented Aug 7, 2021 at 1:53
  • Arrays in C++ don't work this way. Do you know how to use vectors? Commented Aug 7, 2021 at 1:54
  • Check this stackoverflow.com/questions/12009314/… Commented Aug 7, 2021 at 2:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.