2012-05-10 100 views
0

我有添加新的对象指针指针集合的问题。我看不出我做错了什么,为什么我得到这个错误。我不知道我应该如何进行指针数组的初始化。指针和添加对象的集合

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

Main.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); 

}; 
+0

m_tab =新的学生[m_number] – maress

回答

3

*new *Student[m_number]是在错误的位置。您正在创建的Student指针数组和指针批注必须经过Student

m_tab = new Student*[m_number]; 
+0

但如果我给studenci :: Studenci()m_counter = 0的值,这将在无效Studenci :: add(Student * st)中注意到我的意思是在add方法计数器现在的开始值是0? – mathewM

+0

@mathewM我不太清楚问题是什么。该方法看起来正确 – JaredPar