Sonam Chopra
Sonam Chopra

Reputation: 19

Error showing while running a Container class program

Garden contains n apple trees. Every apple tree can be characterized as follows: the harvest of the first year year, increase for every year past, and law according which the number of apples increase every year. The law uses two coefficients coef1 and coef2. The law is common for all the apple trees, but coefficients may differ. You have to:
a) Find out the number of grown apples for every apple tree for the period of years. The number of years is either entered from keyboard or defined as a constant value;
b) Find out the number of grown apples for every apple tree for the specific year;
c) Form new garden from the apple trees that have grown the number of apples during the period of years not less than a defined value. The values of numbers of apples and period of years can be either defined as constants or entered form keyboard.
This is just an initial part and giving errors:

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;
}
//------------------------------------------------------------

Upvotes: 2

Views: 83

Answers (1)

Tas
Tas

Reputation: 7111

In the declaration of Atree, you have declared a method called Print which returns a string. The compiler doesn't know what a string is, and unlike the language C it can't just default the return type to int. You probably mean to use std::string and thus write:

class Atree
{
std::string Print();
};

You may need to #include <string> beforehand.

The compiler is also complaining because your definition of Atree::Print's return type differs. Here it knows you are trying to use std::string because you have brought the std namespace in to context with the using keyword:

using namespace std;
string Atree::Print()
{
}

The compiler knows you wish to return a std::string, but this differs from the Atree::Print declaration, in which the compiler assumes you are trying to return an int.

You should avoid using namespace std. Doing so would've just given you errors about the compiler not knowing what a string is which would've (hopefully) made it easier to solve.

Upvotes: 1

Related Questions