【发布时间】:2021-03-18 03:46:00
【问题描述】:
上下文
我的教授给了我一个任务,让我使用 2 个类之间的聚合来制作一个程序,同时还将这些类分成 .h 和 .cpp 文件。
我的解决方案
包含类声明的头文件:
#include <iostream>
#include <string>
using namespace std;
class medicalCompany {
private:
string ceoName;
string email;
string phoneNumber;
string locate;
public:
medicalCompany();
void Name(string n);
void mail(string m);
void phone(string p);
void location(string l);
~medicalCompany();
};
class origin {
private:
medicalCompany country;
public:
origin();
void address();
~origin();
};
还有我的 .cpp 文件:
#include <iostream>
#include "function.h"
#include <string>
using namespace std;
medicalCompany::medicalCompany() {
cout << "OUR COMPANY IS GLAD TO BE OF SERVICE !" << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::Name(string n){
ceoName = n;
cout << "OUR CEO IS " << endl;
cout<< ceoName << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::mail(string m) {
email = m;
cout << "USE OUR EMAIL TO CONTACT US : " << endl;
cout<< email << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::phone(string p) {
phoneNumber = p;
cout << "THIS IS OUR CUSTOMER SERVICE PHONE NUMBER " << endl;
cout<< phoneNumber << endl;
cout << "****************************************************" << endl;
}
void medicalCompany::location(string l) {
locate = l;
cout << " OUR COMPANY IS LOCATED IN " << endl;
cout << locate << endl;
cout << "****************************************************" << endl;
}
medicalCompany::~medicalCompany() {
cout << "thanks for trusting our company ^_^" << endl;
cout << "****************************************************" << endl;
}
origin::origin() {
cout<< "constructor 2"<<endl;
}
void origin::address() {
cout << country.location;
}
origin::~origin() {
cout << "bye" << endl;
}
这两个类在我的main.cpp文件中使用:
#include <iostream>
#include <string>
#include "function.h"
using namespace std;
int main() {
medicalCompany o;
o.Name("jack");
o.mail("ouremail@company.com");
o.phone("2342352134");
o.location("Germany");
origin o2;
return 0;
}
问题
我遇到了这个错误:
Severity Code Description Project File Line Suppression State
Error C3867 'medicalCompany::location': non-standard syntax; use '&' to create a pointer to member CP2_HW c:\function.cpp 41
【问题讨论】:
-
cout << country.location;location 被声明为void location(string l);,因此它与输出到cout完全不兼容,即使您将其作为函数正确调用也是如此。你是说cout << contry.locate; -
我很确定错误消息是关于我所说的。这一行在这里:
cout << country.location; -
是的,因为 locate 是私有成员。如果您将定位成员作为公共变量,您可以将
void origin::address(){cout << country.location;}替换为void origin::address(){country.location();} or byvoid origin::address(){cout -
将
locate设为公共成员。或者在medicalCompany中添加一个公共函数来返回它。 -
@drescherjm 问题已由 mr 行解决。 arnaud 写的,问题和你说的一样,drescherjm 所以非常感谢你们的帮助^_^
标签: c++ class c++11 aggregation