C++ c++;成员函数声明问题w/类
下面是我目前正在使用的代码的摘录(其余部分与我的问题无关。我在使用信息组合信息(信息a1)时遇到问题)成员函数。我得到一个未在作用域中声明的错误。我所要做的就是合并信息并设置新变量。我能够使用结构成功地实现这一点,现在我正在自学课程C++ c++;成员函数声明问题w/类,c++,class,C++,Class,下面是我目前正在使用的代码的摘录(其余部分与我的问题无关。我在使用信息组合信息(信息a1)时遇到问题)成员函数。我得到一个未在作用域中声明的错误。我所要做的就是合并信息并设置新变量。我能够使用结构成功地实现这一点,现在我正在自学课程 #include <iostream> #include <string> using namespace std; struct Date { int month; int day; int year; };
#include <iostream>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
class Information
{
public:
Information();
void printinformation();
Information combineInfo(Information a1);
//Setters and Getters Here
private:
string a;
double b;
double c;
Date d;
Date e;
};
void initializeDate(Date& d);
void printDate(Date& d);
int main()
{
cout << endl << "Now please input Information #1" << endl;
Information a1; // prompts for all the inputs for a1
cout << endl << "Now please input Information #2" << endl;
Information a2; // prompts for all the inputs for a2
a2.combineInfo(a1); // again prompts for info??
cout << "The combined Information is: " << endl;
info.printinformation();
return 0;
}
Information::Information()
{
string a;
cout << "Please enter a"<<endl;
getline(cin, a);
cout <<"Please enter b?"<<endl;
cin >> b;
getline(cin, dummy);
cout <<"Please enter c?"<<endl;
cin >> c;
getline(cin, dummy);
cout << "Please input the info start dates."<< endl;
initializeDate(start);
cout << "Please input the info end dates."<< endl;
initializeDate(finish);
}
Information Information::combineInfo(Information a1)
{
Information a1;
Information a2;
Information info;
a1.a = a2.a;
//etc.
return info;
}
#包括
#包括
使用名称空间std;
结构日期
{
整月;
国际日;
国际年;
};
班级信息
{
公众:
信息();
作废打印信息();
信息组合信息(信息a1);
//这里有二传手和接球手
私人:
字符串a;
双b;
双c;
日期d;
日期e;
};
无效初始化日期(日期和日期);
无效打印日期(日期和日期);
int main()
{
您是否有:
a2.:combineInfo(a1);
应该是:
a2.combineInfo(a1);
其中有一个额外的“:”是错误的。您的代码给出了许多编译错误,但最奇怪的部分是:
Information a2;
a2.:combineInfo(a1);
// ^^ Remove the :
cout << "The combined Information is: " << endl;
info.printinformation();
// ^^^^
// You didn't declare info
信息a2;
a2.:combineInfo(a1);
//^^删除以下内容:
当我在main
中执行information a2
时,它会提示输入其中的所有信息。然后当我运行a2.combineInfo(a1)时它再次提示输入一些信息。我不知道为什么,它不能这样做。它在我这方面编译得很好,也许我在提取我认为最重要的内容时遗漏了一些关键部分。@user2163231:提示某些信息的代码在信息类的构造函数中。我以为你知道你所做的写在这里,我运行信息a1和信息a2,它通过构造函数提示输入信息,效果很好它再次提示输入一些信息,而不仅仅是合并并设置一些相等的变量。我不知道为什么。@user2163231:您正在构建information
insidecombineInfo()的两个新实例
。所以你调用了两次构造函数。你被询问了两次信息。可能你想通过引用传递。请你在pastebin中向我展示一下你所说的“调用构造函数两次”的大致意思。在这个练习中,我不应该使用通过引用传递。