Garden
包含 n 棵苹果树。每棵苹果树都可以表征如下:第一年的收获,过去每年的增加,以及苹果数量每年增加的规律。该定律使用两个系数 coef1
和 coef2
。该法则适用于所有苹果树,但系数可能不同。你必须:
a) 找出这几年每棵苹果树结出的苹果数量。年数
从键盘输入或定义为常量值;
b) 找出特定年份每棵苹果树结出的苹果数量;
c) 将在这些年内结出一定数量苹果的苹果树形成新的花园
不小于规定值。苹果数量和年限的值可以是
定义为常量或从键盘输入。
这只是初始部分并给出错误:
Warning 3 warning C4183: 'Print': missing return type; assumed to be a member function returning 'int' c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
Warning 6 warning C4183: 'Print': missing return type; assumed to be a member function returning 'int' c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden 9 IntelliSense: no operator "<<" matches these operands operand types are: std::ostream << std::string c:\Users\kumar anubhav\Documents\garden\Source.cpp 42 6 garden
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
Error 7 error C2556: 'std::string Atree::Print(void)' : overloaded function differs only by return type from 'int Atree::Print(void)' c:\users\kumar anubhav\documents\garden\atree.cpp 9 1 garden
Error 8 error C2371: 'Atree::Print' : redefinition; different basic types c:\users\kumar anubhav\documents\garden\atree.cpp 9 1 garden
Error 1 error C2146: syntax error : missing ';' before identifier 'Print' c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
Error 4 error C2146: syntax error : missing ';' before identifier 'Print' c:\users\kumar anubhav\documents\garden\atree.h 11 1 garden
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 trees\n";
cout << " Coef1 coef2 year increa\n";
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
因为你带来了std
命名空间与 using
的上下文关键词:
using namespace std;
string Atree::Print()
{
}
编译器知道你希望返回一个 std::string
, 但这不同于 Atree::Print
声明,其中编译器假定您正在尝试返回 int
.
你应该避免 using namespace std
.这样做只会给你关于编译器不知道什么是 string
的错误。这会(希望)让它更容易解决。
关于c++ - 运行容器类程序时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687137/