[英]How do I get my constructor and functions to work so my main() is able to display both the string and int data?
I am learning about functions and classes, and wrote my own code. 我正在学习函数和类,并编写了自己的代码。 I used the constructor to just initialize the variables.
我使用了构造函数来初始化变量。 I have a function that is supposed to get the info I initialized with the constructor and allow me to display it.
我有一个功能,该功能应该获取我用构造函数初始化的信息,并允许我显示它。 However, it doesn't want to work.
但是,它不想工作。 I am not really sure what I am doing wrong.
我不太确定自己在做什么错。 My error code says that I have unresolved externals because of my "void" function.
我的错误代码表示由于我的“无效”功能,我无法解析外部因素。 I thought my function was not returning anything but rather just displaying the input it got from the initialization of the constructor.
我以为我的函数不会返回任何东西,而只是显示从构造函数的初始化中获得的输入。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Berries {
string Nameofberries;
int Price;
public:
Berries (string N,int B)
{
Nameofberries = N;
Price = B;
}
void GetBerryInfo(const Berries& B)
{
cout << B.Nameofberries << endl;
cout << B.Price << endl;
}
};
void GetBerryInfo (const Berries& B);
int main ()
{
Berries Berryinfo1( "Raspberries", 7);
cout << GetBerryInfo;
system("pause");
return 0;
}
There are several mistakes. 有几个错误。
void GetBerryInfo(const Berries& B)
{
cout << B.Nameofberries << endl;
cout << B.Price << endl;
}
should be 应该
void GetBerryInfo()
{
cout << Nameofberries << endl;
cout << Price << endl;
}
================================================================== ================================================== ================
void GetBerryInfo (const Berries& B);
should be removed. 应该删除。
================================================================== ================================================== ================
cout << GetBerryInfo;
should be 应该
Berryinfo1.GetBerryInfo();
================================================================== ================================================== ================
All computer langauges are fussy, you have to get the details right, as well as understand the concepts. 所有计算机语言都很挑剔,您必须正确了解细节并理解概念。
This will do what you wanted: 这将完成您想要的操作:
# include <iostream>
# include <iomanip>
# include <string>
using namespace std;
class Berries {
string Nameofberries;
int Price;
public:
Berries (string N,int B)
{
Nameofberries = N;
Price = B;
}
void GetBerryInfo()
{
cout << Nameofberries << endl;
cout << Price << endl;
}
};
int main ()
{
Berries Berryinfo1( "Raspberries", 7);
Berryinfo1.GetBerryInfo();
system("pause");
return 0;
}
A couple of points on your mistakes: 有关您的错误的几点要点:
GetBerryInfo()
was declared inside the class. GetBerryInfo()
在类内部声明。 You don't need to re-declare it in the global scope. GetBerryInfo
) must have ()
at the end of them like so: GetBerryInfo()
. GetBerryInfo
)必须在其末尾具有()
,例如: GetBerryInfo()
。 GetBerryInfo()
to take Berries
as a paremeter. GetBerryInfo()
没有必要将Berries
作为参数。 It is a member function that is part of the class Berries
. Berries
类的一部分的成员函数。 It has access to all data members of a Berries
instance already. Berries
实例的所有数据成员。 cout
here: cout << GetBerryInfo;
cout
: cout << GetBerryInfo;
because the function body already sends the data members to cout
. cout
。 This function returns void
so it doesn't make sense to send this to cout
anyway. void
因此无论如何将其发送给cout
没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.