我有三个文件,Person.h
,Person.cpp
和我的main.cpp
。在Person.h
我有我的所有类,而.cpp部分包含实际的实现。我的问题是,当我尝试读取由我的一个函数创建并由私有数据成员*主题指向的数组时,我收到两条错误消息。
错误消息显示为:
严重级代码说明项目文件行 错误C3867'Student :: myarray1':非标准语法;使用'&'至 创建指向成员lab_10的指针c:\ users \ shireen \ documents \ visual studio 2015 \ projects \ lab_10 \ lab_10 \ main.cpp 67
严重级代码说明项目文件行错误C2109下标 需要数组或指针 输入lab_10 c:\ users \ shireen \ documents \ visual studio 2015 \ projects \ lab_10 \ lab_10 \ main.cpp 67
和我的实际代码。
的main.cpp
#include"Person.h"
#include<string>
#include<iostream>
using namespace std;
int main()
{
Student *pointer1;
Staff *pointer2;
string name, gender;
int age, studentno, subs;
pointer1 = new Student[4];
pointer2 = new Staff[2];
for (int i = 1; i < 5; i++)
{
cout << "please enter the name of student " << i << endl;
cin >> name;
cout << "please enter the age of student " << i << endl;
cin >> age;
cout << "please enter the gender of student " << i << endl;
cin >> gender;
cout << "please enter student " << i << "'s student number" << endl;
cin >> studentno;
cout << "please enter the number of subjects taken by student " << i << endl;
cin >> subs;
pointer1[i].set_name(name);
pointer1[i].set_age(age);
pointer1[i].set_gender(gender);
pointer1[i].set_StudentNumber(studentno);
pointer1[i].set_NumberOfSubjects(subs);
pointer1[i].myarray1(subs);
}
for (int i = 1; i < 3; i++)
{
cout << "please enter the name of Staff member " << i << endl;
cin >> name;
cout << "please enter the age of staff member " << i << endl;
cin >> age;
cout << "please enter the gender of staff member " << i << endl;
cin >> gender;
cout << "please enter the number of subjects taught by staff member " << i << endl;
cin >> subs;
pointer2[i].set_name(name);
pointer2[i].set_age(age);
pointer2[i].set_gender(gender);
pointer2[i].set_NumberOfSubjects(subs);
pointer2[i].myarray2(subs);
}
for (int i = 1; i < 5; i++)
{
pointer1[i].get_name();
pointer1[i].get_StudentNumber();
cout << "Student name: " << name << endl;
cout << "Student age: " << age << endl;
for (int j = 0; j < 4; j++)
{
string *word;
pointer1[i].myarray1[j] = word;
cout << word << endl;
}
}
system("PAUSE");
return 0;
Person.h
#ifndef _PERSON_H_
#define _PERSON_H_
#include <string>
#include <iostream>
using namespace std;
class Person
{
private:
string name;
int age;
string gender;
public:
Person();
~Person();
void set_name(string n);
void set_age(int a);
void set_gender(string g);
string get_name();
int get_age();
string get_gender();
};
class Staff : public Person
{
private:
int NumberOfSubjects;
string *Subjects;
public:
Staff();
~Staff();
void set_NumberOfSubjects(int ns);
int get_NumberOfSubjects();
string myarray2(int ns);
};
class Student : public Person
{
private:
int StudentNumber;
int NumberOfSubjects;
string *Subjects;
public:
Student();
~Student();
bool set_StudentNumber(int sn);
int get_StudentNumber();
void set_NumberOfSubjects(int ns);
int get_NumberOfSubjects();
string myarray1(int ns);
};
#endif _PERSON_H
Person.cpp
#include "Person.h"
Person::Person() {}
void Person::set_name(string n)
{
name = n;
}
void Person::set_age(int a)
{
age = a;
}
void Person::set_gender(string g)
{
gender = g;
}
string Person::get_name() { return name; }
int Person::get_age() { return age; }
string Person::get_gender() { return gender; }
Person::~Person() {}
//staff
Staff::Staff() {}
void Staff::set_NumberOfSubjects(int ns)
{
NumberOfSubjects = ns;
}
int Staff::get_NumberOfSubjects() { return NumberOfSubjects; }
string Staff::myarray2(int NumberOfSubjects)
{
Subjects = new string[NumberOfSubjects];
for (int i = 0; i < NumberOfSubjects; i++)
{
cout << "please enter the number of subjects taught" << endl;
cin >> Subjects[i];
return Subjects[i];
}
}
Staff::~Staff()
{
delete[] Subjects;
}
///student
Student::Student() {}
bool Student::set_StudentNumber(int sn)
{
StudentNumber = sn;
if (sn<0 || 100>sn)
{
cout << "This is not a valid student number" << endl;
system("PAUSE");
return false;
}
}
int Student::get_StudentNumber() { return StudentNumber; }
void Student::set_NumberOfSubjects(int ns)
{
cout << "Please enter the number of subjects: " << endl;
cin >> ns;
NumberOfSubjects = ns;
}
int Student::get_NumberOfSubjects() { return NumberOfSubjects; }
string Student::myarray1(int NumberOfSubjects)
{
string *sub_Ptr;
Subjects = new string[NumberOfSubjects];
for (int i = 0; i < NumberOfSubjects; i++)
{
cout << "Please enter the name of the subject: " << endl;
cin >> Subjects[i];
sub_Ptr = &Subjects[i];
return *sub_Ptr;
}
}
Student::~Student()
{
delete[] Subjects;
}
答案 0 :(得分:0)
第67行 - Main.cpp
pointer1[i].myarray1[j] = word;
不太确定你在这里尝试做什么但由于pointer1 [i]返回Student
,错误正在访问myarray1[j]
。 Person
和Student
都没有公开myarray1
数组,因此不允许这样做。
但是有一个功能:string myarray1(int ns);
Student
你打算打电话给它吗?如果您打算访问指针string *Subjects
,那么您必须通过函数或公开(不推荐)来提供对它的访问。
如果我理解你正在尝试:
// For each student
for (int i = 1; i < 5; i++) {
// Print some information about the student
cout << "Student name: " << pointer1[i].get_name() << endl;
cout << "Student age: " << pointer1[i].get_age() << endl;
cout << "Student number: " << pointer1[i].get_StudentNumber() << endl;
// Obtain all the subjects the student has
string* subjects = pointer1[i].AllSubjects();
for (int j = 0; j < pointer1[i].get_NumberOfSubjects(); j++) {
// Here you want to print the name of the subjects the student is taking (?)
cout << "Course #" << (j + 1) << ": " << subjects[j] << std::endl;
}
}
如果您在主要版本中使用此功能,则必须在Student
课程中添加一项功能:
string* Student::AllSubjects() { return Subjects; }
输出如下:
Student name: <name>
Student age: <age>
Student number: <number>
Course #1: <course1>
Course #2: <course2>
旁注:
std::shared_ptr<T>
,std::unique_ptr<T>
和朋友std::vector<T>
然后使用指针数组(以及更多类似C ++)要容易得多。答案 1 :(得分:0)
我猜问题就在这一行
pointer1[i].myarray1[j] = word;
myarray1 是一个函数,你需要传递参数,可能就是你的意思
pointer1[i].myarray1(j) = *word;
另请注意,字是指向字符串的指针,您尝试将其分配给字符串。
答案 2 :(得分:0)
这些是我建议让您的代码有效的更改(但还有更多问题)
学生标题(此函数现在返回一个包含[ns]元素的字符串数组
string * myarray1(int ns);
studen cpp
此函数只会分配一次主题数组,并会要求用户输入主题[x]次。
string * Student::myarray1(int NumberOfSubjects)
{
Subjects = new string[NumberOfSubjects];
for (int i = 0; i < NumberOfSubjects; i++)
{
cout << "Please enter the name of the subject: " << endl;
cin >> Subjects[i];
}
return Subjects;
}
一旦我们得到了所有的主题,现在我们将它们循环(全部4个)并打印出来。
希望这是你要做的......
主
for (int i = 1; i < 5; i++)
{
pointer1[i].get_name();
pointer1[i].get_StudentNumber();
cout << "Student name: " << name << endl;
cout << "Student age: " << age << endl;
string *word = pointer1[i].myarray1(4);
for (int j = 0; j < 4; j++)
cout << word[j] << endl;
}
答案 3 :(得分:0)
小问题:
#endif //_PERSON_H_
#include <stdlib.h>