0

我在将新对象指针添加到指针集合时遇到了问题。我看不出我做错了什么以及为什么会出现此错误。我不确定我应该如何初始化指针数组。

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

主文件

#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
}

学生.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 回答