简体   繁体   English

如何使我的构造函数和函数起作用,以便我的main()能够同时显示字符串和int数据?

[英]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. 您无需在全局范围内重新声明它。 That 2nd declaration should be removed. 该第二条声明应删除。
  • To be invoked, functions (like GetBerryInfo ) must have () at the end of them like so: GetBerryInfo() . 要被调用,函数(如GetBerryInfo )必须在其末尾具有() ,例如: GetBerryInfo()
  • There is no point for 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实例的所有数据成员。
  • You don't need to use cout here: cout << GetBerryInfo; 您无需在此处使用coutcout << 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.

相关问题 如何获得在布尔语句中定义的变量以在主程序中工作? - How do i get the variable defined in my boolean statement to work in my main program? 我将如何检查 main 中的布尔变量,以便我的 do-while 循环遍历三个函数? - How would i check the boolean variable in main so my do-while loop goes through the three functions? 我如何从无效中脱离当前范围,以便回到主要领域? 我需要做什么? - How do I get out of my current scope from void so I can get back to main? What do I need to do? 如何让我的 header、function 和主文件一起工作,我总是得到:错误:ID 返回 1 退出状态 - How do I make my header, function and main file work together, I always get: error: Id returned 1 exit status 我正确地构建这些引用和指针函数吗? 如果是这样,我如何明确地调用我的析构函数? - Am I building these reference and pointer functions correctly? And if so, how do I explicitly call my destructor? 我如何让这个构造函数工作? - How do I get this constructor to work? 如何分发C ++ Allegro 5程序,以便它们在未安装Allegro的计算机上运行? - How do I distribute my C++ Allegro 5 programs so that they work on computers without Allegro installed? 如何将字符串添加到我的 struct 类型数组中,该数组同时包含 C++ 中的字符串和整数? - How do I add a string to my array of type struct which contains both strings and ints in c++? 我从我的字符串函数返回到主函数是什么? - What do I return to the main function from my string function? 为什么我的推回功能不起作用,只会出现段错误 - How come my push back functions do not work and just get a seg fault
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM