[英]C++ Class w/ struct 'undeclared identifier"
So I am getting an error that my privates are not being declared when in use.所以我收到一个错误,我的私人在使用时没有被声明。 I feel like my initialization in my cpp file might be the problem but there is nothing that I can see is wrong.
我觉得我在 cpp 文件中的初始化可能是问题所在,但我看不出有什么问题。
The errors I am getting are:我得到的错误是:
main.cpp:55:35: error: use of undeclared identifier 'MailingAddress'
testStudent.setMailingAddress(MailingAddress);
main.cpp:56:36: error: use of undeclared identifier 'PhysicalAddress'
testStudent.setPhysicalAddress(PhysicalAddress);
This is my Student.h file.这是我的 Student.h 文件。
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
struct Address
{
string street, city, state, zip;
};
class Student{
private:
string name;
Address MailingAddress;
Address PhysicalAddress;
double age;
public:
Student();
Student(string name, Address MailingAddress,Address PhysicalAddress, double age);
~Student();
void setName(string iname);
void setMailingAddress(Address iMailingAddress);
void setPhysicalAddress(Address iPhysicalAddress);
void setAge(double iage);
string getName();
Address getMailingAddress();
Address getPhysicalAddress();
double getAge();
};
#endif
This is my .cpp file.这是我的 .cpp 文件。
#include "Student.h"
#include <string>
//INITIALIZATION
Student::Student(string n,Address mA,Address pA,double a)
{
name = n;
MailingAddress = mA;
PhysicalAddress = pA;
age = a;
}
Student::~Student()
{
cout << "Instance removed from memory" << endl;
}
//SETTERS
void Student::setName(string name)
{name = name;}
void Student::setMailingAddress(Address MailingAddress)
{MailingAddress = MailingAddress;}
void Student::setPhysicalAddress(Address PhysicalAddress)
{PhysicalAddress = PhysicalAddress;}
void Student::setAge(double age)
{age = age;}
//GETTERS
string Student::getName()
{return name;}
Address Student::getMailingAddress()
{return MailingAddress;}
Address Student::getPhysicalAddress()
{return PhysicalAddress;}
double Student::getAge()
{return age;}
And my main.cpp还有我的 main.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main() {
double age;
string name;
string street0, city0, state0, zip0, street1, city1, state1, zip1;
cout << "Enter the student's name: ";
cin >> name;
cout << "Enter the student's age: ";
cin >> age;
cout << "Enter the student's mailing address (street, city, state, zip): ";
cin >> street0 >> city0 >> state0 >> zip0;
cout << "Enter the student's physical address (street, city, state, zip): ";
cin >> street1 >> city1 >> state1 >> zip1;
// Add the proper lines of codes to create an instance of type Student. Name this instance testStudent
Student testStudent;
testStudent.setName(name);
testStudent.setMailingAddress(MailingAddress);
testStudent.setPhysicalAddress(PhysicalAddress);
testStudent.setAge(age);
// Note the next few lines of code will not compile until testStudent is declared and initialized
// Printing using accessors
cout << "Student Info:" << endl;
cout << "\tName: " << testStudent.getName() << endl;
cout << "\tAge: " << testStudent.getAge() << endl;
cout << "Mailing Address: " << endl << "\t" << testStudent.getMailingAddress().state << endl
<< "\t" << testStudent.getMailingAddress().city << endl
<< "\t" << testStudent.getMailingAddress().state << endl
<< "\t" << testStudent.getMailingAddress().zip << endl;
cout << "Physical Address:" << endl << "\t" << testStudent.getPhysicalAddress().state << endl
<< "\t" << testStudent.getPhysicalAddress().city << endl
<< "\t" << testStudent.getPhysicalAddress().state << endl
<< "\t" << testStudent.getPhysicalAddress().zip << endl;
return 0;
}
This:这个:
testStudent.setMailingAddress(MailingAddress);
testStudent.setPhysicalAddress(PhysicalAddress);
does not make sense.没有意义。
MailingAddress
and PhysicalAddress
are your private members in testStudent
of type Address
. MailingAddress
和PhysicalAddress
是您在类型为Address
testStudent
的私有成员。
You have to create an Address
first, from the data you got from the user, and then pass it to the set*
methods.您必须首先根据从用户那里获得的数据创建一个
Address
,然后将其传递给set*
方法。
Error message is quite clear here.错误信息在这里很清楚。 In scope of main MailingAddress and PhysicalAddress are not available.
在主要 MailingAddress 和 PhysicalAddress 范围内不可用。
Couple of points about code sample关于代码示例的几点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.