首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行容器类程序时显示错误

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

Stack Overflow用户
提问于 2015-07-28 21:09:12
回答 1查看 77关注 0票数 2

Garden含有n棵苹果树。每棵苹果树的特点是:第一年的收获,过去每年的增长,以及苹果数量逐年增加的规律。该定律采用两个系数coef1coef2。对于所有的苹果树来说,这种规律是普遍的,但是系数可能不同。你必须:

( a)找出几年来每棵苹果树种植的苹果数。从键盘输入的年数或定义为常量值;

b)找出特定年份每棵苹果树种植的苹果数;

( c)从苹果树中形成新的花园,这些苹果树在数年内种植的苹果数量不少于规定的值。苹果数和年数的值可以定义为常量,也可以用键盘输入。

这只是一个最初的部分,并产生了错误:

警告3警告C4183:“打印”:缺少返回类型;假定为成员函数返回'int‘c:\users\kumar \documents\花园\atree.h111花园 警告6警告C4183:“打印”:缺少返回类型;假定为返回'int‘c:\users\kumar anubhav\documents\ anubhav\Documents\garden\Source.cpp \atree.h 11 1花园9 IntelliSense:无运算符" <<“匹配这些操作数,类型为: std::ostream << std::string c:\anubhav\Documents\garden\Source.cpp 42 anubhav\Documents\garden\Source.cpp 42花园 错误2错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int:\user\kumar\documents\花园\atree.h111花园 错误5错误C4430:缺少类型说明符- int假设。注意: C++不支持默认-int:\user\kumar\documents\花园\atree.h111花园 错误7错误C2556:'std::string::Print(Void)‘:重载函数的不同之处在于返回类型与'int::Print(Void)’c:\user\kumar anubhav\documents\garden\atree.cpp 9 1花园 错误8错误C2371:'Atree::Print‘:重新定义;不同的基本类型c:\user\kumar anubhav\documents\garden\atree.cpp 9 1花园 错误1错误C2146:语法错误:“缺少”;“在标识符”打印之前“c:\user\kumar\documents\花园\atree.h111花园 错误4错误C2146:语法错误:“缺少”;“在标识符”打印之前“c:\user\kumar\documents\花园\atree.h111花园

代码语言:javascript
复制
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;
}
//------------------------------------------------------------
EN

回答 1

Stack Overflow用户

发布于 2015-07-28 21:30:43

Atree的声明中,您已经声明了一个名为Print的方法,它返回一个string。编译器不知道string是什么,而且与C语言不同的是,它不能只是默认返回到int。您可能打算使用std::string,从而编写:

代码语言:javascript
复制
class Atree
{
std::string Print();
};

您可能需要预先#include <string>

编译器也在抱怨,因为您对Atree::Print的返回类型的定义不同。在这里,它知道您试图使用std::string,因为您用using关键字将std命名空间引入上下文:

代码语言:javascript
复制
using namespace std;
string Atree::Print()
{
}

编译器知道您希望返回一个std::string,但是这与Atree::Print声明不同,在该声明中编译器假定您试图返回一个int

您应该避免使用using namespace std。这样做只会给您带来关于编译器的错误,不知道string是什么,这将(希望)使它更容易解决。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31687137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档