运行容器类程序时显示错误

Error showing while running a Container class program

本文关键字:显示 错误 程序 容器类 运行      更新时间:2023-10-16

Garden包含 n 棵苹果树。每棵苹果树的特征如下:第一年的收获,过去的每一年增加,以及苹果数量每年增加的规律。该定律使用两个系数coef1coef2。该定律对所有苹果树都是通用的,但系数可能不同。您必须:
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的错误,这将(希望(更容易解决。