Hi everybody,
I am learning C++, and I ran into some trouble. But I can't figure out exactly what it is I am doing wrong.
I have included my code, and the list of compiler errors. (There are 29 of them, but I suspect they are mostly related to one major error).
Thanks for your help in advance!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include "stdafx.h"
#include <iostream>
using namespace std ;
class tree
{
public:
tree(int age, int sugar, int leaves); //constuctor
tree();
~tree();
int getage();
void setage(int age);
int getsugar() ;
void setsugar (int sugar) ;
int getleaves() ;
void setleaves (int leaves) ;
void growleave (int n) ;
void dfosy (int n) ; //dag fotosynthese
void nfosy (int n) ; //nacht fotosynthese
void showgrowspeed ();
protected:
int itsage ;
int itssugar ;
int itsleaves ;
int growspeed ; //virtual
int maxleaves (50) ; //constant?
}
tree::tree (int age, int sugar, int leaves)
{
itsage = age ;
itssugar = sugar ;
itsleaves = leaves ;
}
tree::tree ()
{
itsage = 1 ;
itssugar = 5 ;
itsleaves = 1 ;
}
int tree::getage()
{
return itsage ;
}
void tree::setage(int age)
{
itsage = age ;
}
int tree::getsugar()
{
return itssugar ;
}
void tree::setsugar(int sugar)
{
itssugar = sugar ;
}
int tree::getleaves()
{
return itsleaves ;
}
void tree::setleaves(int leaves)
{
itsleaves = leaves ;
}
void tree::growleave (int n)
{
while (n != 0)
{
itssugar = itssugar -5 ;
itsleaves++ ;
n-- ;
}
}
void tree::dfosy (int n)
{
while (n != 0) //geen +n (als n >1)gebruikt, ivm toevoegen functies.
{
itssugar = itssugar + itsleaves ;
n-- ;
}
}
void tree::nfosy (int n)
{
while (n != 0) //geen +n (als n >1)gebruikt, ivm toevoegen functies.
{
itssugar = itssugar - itsleaves ;
n-- ;
}
}
void tree::showgrowspeed ()
{
cout << "The growspeed is: " << growspeed << ". " ;
}
class oak : public tree //afleiding eik klasse
{
public:
protected:
int growspeed = 2 ;
}
class maple : public tree //afleiding esdoorn klasse
{
public:
protected:
int growspeed = 3 ;
}
class ash : public tree //afleiding es klasse
{
public:
protected:
int growspeed = 2 ;
}
;
int main ()
{
int choise ;
cout << "Please enter a number to create a tree. \nPress 1 to create an Oak tree \nPress 2 to create a Maple tree \nPress 3 to create an Ash tree \n" ;
cin >> choise ;
if (choise == 1)
{
oak *ptree01 = new oak ;
cout << "\nTree 1, Oak, succesfully created.\n\n" ;
}
else if (choise == 2)
{
maple *ptree01 = new maple ;
cout << "\nTree 1, Maple, succesfully created.\n\n" ;
}
else if (choise == 3)
{
ash *ptree01 = new ash ;
cout << "\nTree 1, Ash, succesfully created.\n\n" ;
}
else
{
cout << "\nNo valid input received.\n" ;
}
cout << "Tree 1 is " << *ptree01->getage() << " years old.\n" ;
int treechoise ;
int menuchoise ;
int noa ; //number of actions
bool menu = true ;
while (menu)
{
cout << "\nPlease select a tree (0, 1 or 2)\n" ;
cin >> treechoise ;
cout << "\nplease select an event\n" << "Press 1 to see sugar level\nPress 2 to see number of leaves\nPress 3 to grow a new leaf\nPress 4 to start photosynthesis\nPress 5 to start night mode\nPress 6 to see the grow speed\nPress 7 to see the age\nPress 8 to exit.\n" ;
if (menuchoise == 1)
{
cout << "\nThe tree has " << (*ptree01).getsugar() << " sugar.\n" ;
break;
}
else if (menuchoise == 2)
{
cout << "\nThe tree has " <<*ptree01->getleaves() << " leaves.\n" ;
break;
}
else if (menuchoise == 3)
{
cout << "\nGrowing a new leave. \n" ;
*ptree01->growleave(1) ;
break ;
}
else if (menuchoise == 4)
{
cout << "\nStarting photosynthesis. \n" << "How many times should photosynthesis take place?\n" ;
cin >> noa ;
*ptree01->dfosy(noa) ;
break ;
}
else if (menuchoise == 5)
{
noa = 0 ;
cout << "\nStarting night. \n" << "How many night should take place?\n" ;
cin >> noa ;
*ptree01->nfosy(noa) ;
break ;
}
else if (menuchoise == 6)
{
cout << "\nThe growspeed is " ;
*ptree01->showgrowspeed() ;
cout << ".\n" ;
break ;
}
else if (menuchoise == 7)
{
cout << "\nThe tree is " << ptree01->getage << " years old.\n";
break ;
}
else
{
cout << "\nExiting\n" ;
menu = false ;
}
}
return (0) ;
}
|
path(39) : error C2059: syntax error : 'constant'
path(43) : error C2533: 'tree::{ctor}' : constructors not allowed a return type
path(125) : error C2864: 'oak::growspeed' : only static const integral data members can be initialized within a class
path(128) : error C2236: unexpected 'class' 'maple'. Did you forget a ';'?
path(128) : error C2143: syntax error : missing ';' before ':'
path(128) : error C2059: syntax error : ':'
path(128) : error C2059: syntax error : 'public'
path(129) : error C2143: syntax error : missing ';' before '{'
path(129) : error C2447: '{' : missing function header (old-style formal list?)
path(141) : error C2864: 'ash::growspeed' : only static const integral data members can be initialized within a class
path(159) : error C2065: 'maple' : undeclared identifier
path(159) : error C2065: 'ptree01' : undeclared identifier
path(159) : error C2061: syntax error : identifier 'maple'
path(174) : error C2065: 'ptree01' : undeclared identifier
path(174) : error C2227: left of '->getage' must point to class/struct/union/generic type
type is ''unknown-type''
path(189) : error C2065: 'ptree01' : undeclared identifier
path(189) : error C2228: left of '.getsugar' must have class/struct/union
type is ''unknown-type''
path(194) : error C2065: 'ptree01' : undeclared identifier
path(194) : error C2227: left of '->getleaves' must point to class/struct/union/generic type
type is ''unknown-type''
path(200) : error C2065: 'ptree01' : undeclared identifier
path(200) : error C2227: left of '->growleave' must point to class/struct/union/generic type
type is ''unknown-type''
path(207) : error C2065: 'ptree01' : undeclared identifier
path(207) : error C2227: left of '->dfosy' must point to class/struct/union/generic type
type is ''unknown-type''
path(215) : error C2065: 'ptree01' : undeclared identifier
path(215) : error C2227: left of '->nfosy' must point to class/struct/union/generic type
type is ''unknown-type''
path(221) : error C2065: 'ptree01' : undeclared identifier
path(221) : error C2227: left of '->showgrowspeed' must point to class/struct/union/generic type
type is ''unknown-type''
path(227) : error C2065: 'ptree01' : undeclared identifier
path(227) : error C2227: left of '->getage' must point to class/struct/union/generic type
type is ''unknown-type''