0

ポインターのコレクションに新しいオブジェクト ポインターを追加する際に問題が発生しました。何が間違っていたのか、なぜこのエラーが発生したのかわかりません。ポインター配列の初期化をどのように行うべきかわかりません。

studenci.cpp: In constructor `Studenci::Studenci()':
studenci.cpp:10: error: expected identifier before '*' token
studenci.cpp:10: error: expected `;' before "Student"

メイン.cpp

#include <iostream>
#include "student.h"
#include "studenci.h"

using namespace std;

int main(int argc, char *argv[])
{


Studenci *s = new Studenci(3); //My collection

Student *s1 = new Student("s1","Adam", "Smyk", "82736372821", "s1020", 22, 1, true);  //First object
Student *s2 = new Student("s2","Arnold", "Smyk", "82736372823", "s1021", 22, 1, true);
Student *s3 = new Student("s3","Arnold", "Smyk", "82736372822", "s1031", 24, 1, false);

s1->show();
s2->show();
s3->show(); //Showing content

s->add(s1); //I think here is a problem with adding new pointers 
    s->add(s2)->add(s3);

    return 0;
}

Studenci.cpp

#include "student.h"
#include "studenci.h"
#include <iostream>
using namespace std;

Studenci::Studenci()
{
        m_tab = new *Student[m_number];  //Is it correct ? 
        m_counter = 0; 
}


void Studenci::add(Student* st)
{
    if( m_counter < m_number){

    m_tab[m_counter] = new Student(*st);  
    m_counter++;
}else
        cout<<"Full!"<<endl;    //error, no more space
}

Studenci.h

class Studenci{

    public:
    Studenci(); //default

    Studenci(int number):m_number(number)
    {} 
    public:
        int m_number;
        int m_counter;
        Student **m_tab; 

    //Function of adding
    void add(Student* st);

};
4

1 に答える 1

3

*あなたのnew *Student[m_number]は間違った場所にあります。ポインターの配列を作成してStudentおり、ポインター表記は後にある必要がありますStudent

m_tab = new Student*[m_number];
于 2012-05-10T16:24:46.653 に答える