运行容器类程序时显示错误
Error showing while running a Container class program
Garden
包含 n 棵苹果树。每棵苹果树的特征如下:第一年的收获,过去的每一年增加,以及苹果数量每年增加的规律。该定律使用两个系数coef1
和coef2
。该定律对所有苹果树都是通用的,但系数可能不同。您必须:
a( 找出每棵苹果树在几年内种植的苹果数量。年数从键盘输入或定义为常量值;
b( 找出特定年份每棵苹果树种植的苹果数量;
c( 从多年来增加苹果数量的苹果树形成新的花园不小于定义的值。苹果数量和年份的值可以是定义为常量或输入的键盘表单。
这只是一个初始部分,并给出错误:
警告3 警告 C4183:"打印":缺少返回类型;假定是返回"int"的成员函数 c:\users\kumar anubhav\documents\garden\atree.h 11 1 个花园
警告6 警告 C4183:"打印":缺少返回类型;假定是返回"int"的成员函数 C:\users\kumar anubhav\documents\garden\atree.h 11 1 个花园 9 智能感知:没有运算符"<<"与这些操作数匹配 操作数类型为: std::ostream <<std::string c:\Users\kumar anubhav\Documents\garden\Source.cpp 42 6 Garden
错误2 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持 default-int c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
错误5 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持 default-int c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
错误 7 错误 C2556: 'std::字符串 Atree::P rint(void(' :重载函数仅在返回类型上与 'int Atree::P rint(void(' 不同 c:\users\kumar anubhav\documents\garden\atree.cpp 9 1 花园
错误8 错误 C2371:"Atree::P rint":重新定义;不同的基本类型 C:\用户\库马尔·阿努巴夫\文档\花园\atree.cpp 9 1 个花园
错误1 错误 C2146:语法错误:标识符"打印"之前缺少";" c:\users\kumar anubhav\documents\garden\atree.h 11 1 个花园
错误4 错误 C2146:语法错误:标识符"打印"之前缺少";" c:\users\kumar anubhav\documents\garden\atree.h 11 1 个花园
class Atree
{
private:
int year, increa;
int coef1, coef2;
public:
Atree(): year(0), increa(16), coef1(1), coef2(2) { }
Atree(int year, int increa, int coef1, int coef2):
year(year), increa(increa), coef1(coef1), coef2(coef2) { }
string Print();
};
#include "Atree.h"
#include <sstream>
#include <iomanip>
using namespace std;
//------------------------------------------------------------
// Writes data into a line
string Atree::Print()
{
stringstream sr;
sr << setw(6) << coef1 << setw(6) << coef2
<< setw(5) << year << setw(7) << increa ;
return sr.str();
}
#pragma once
//------------------------------------------------------------
#include "Atree.h"
class Garden
{
public:
static const int CMaxi = 100;
private:
Atree Atrees[CMaxi];
int n;
public:
Garden():n(0) { }
Atree Get(int i) { return Atrees[i]; }
int Get() { return n; }
void Set(Atree ob) { Atrees[n++] = ob; }
};
//------------------------------------------------------------
#include "Garden.h"
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
//------------------------------------------------------------
const char Cu1[]="U1.txt";
//------------------------------------------------------------
void Read(Garden & Gard, const char fv []);
void Print(Garden & Gard);
//------------------------------------------------------------
int main()
{
Garden Gard;
Read(Gard, Cu1);
Print(Gard);
return 0;
}
//------------------------------------------------------------
// Reads data from the file fv
void Read(Garden & Gard, const char fv [])
{
ifstream fd(fv);
int n;
fd >> n;
int coef1, coef2, year, increa;
for (int i = 0; i < n; i++) {
fd >> coef1 >> coef2 >> year >> increa;
Gard.Set(Atree(year, increa, coef1, coef2));
}
fd.close();
}
//------------------------------------------------------------
// Prints the data of object Gard
void Print(Garden & Gard)
{
cout << " Information on apple treesn";
cout << " Coef1 coef2 year increan";
for (int i = 0; i < Gard.Get(); i++)
cout << Gard.Get(i).Print() << endl;
}
//------------------------------------------------------------
在 Atree
的声明中,你声明了一个名为 Print
的方法,它返回一个string
。编译器不知道string
是什么,与语言 C 不同,它不能将返回类型默认为 int
。您可能打算使用std::string
,因此可以编写:
class Atree
{
std::string Print();
};
您可能需要事先#include <string>
。
编译器也在抱怨,因为你对Atree::Print
返回类型的定义不同。在这里,它知道您正在尝试使用std::string
因为您已使用 using
关键字将std
命名空间带入上下文中:
using namespace std;
string Atree::Print()
{
}
编译器知道您希望返回std::string
,但这与Atree::Print
声明不同,在声明中,编译器假定您正在尝试返回int
。
您应该避免using namespace std
.这样做只会给你带来关于编译器不知道什么是string
的错误,这将(希望(更容易解决。
- 显示错误输出的简单数组排序程序
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- Clion显示错误,但可以使用Cmake成功构建代码
- 代码使用向量成功运行,但使用数组显示错误
- SDL 映像:无法打开映像,仅显示错误消息
- 为什么将指针重新分配给 1D 数组的 char 时显示错误,但 2D 数组工作正常?
- 与clang++一起使用的VS代码在构建良好的C++文件中显示错误
- 编译器没有显示错误,但我没有得到任何输出.怎么了?
- PPM 颜色显示错误
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- is_permutation方法从 #include<algorithm> 显示错误的输出
- 为什么使用与父级声明的 std::function 在与子级一起使用时会显示错误?
- 我的代码厨师提交显示错误的结果
- 显示错误"std_lib_facilities.h"
- 为什么C++显示错误的数字?
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- 这是我的 .h 文件的一些错误,当我在其中包含我的类模板时,它显示 [错误] 未终止 #ifndef
- c++ IF 语句执行,但添加 ELSE 会显示错误
- 内核.cpp在制作 kernel.o 时显示错误和 Makefile 错误
- C++代码编译,但在 Zorin OS 上运行时显示错误