Okay so I'm writing a simple program - so far with just 1 header and 1 .cpp file to go with it. I'm getting strange errors saying that my struct hasn't been recognised even though I declare it in the header. The code involved is --
Code:
#include<stdio.h>
#include<iostream>
#include<sstream>
#include"bots.h"
//#include"prisonersDilemna.h"
//write program to battle multiple bots with a random choice generator
//and after all iterations post who comes out on top.
//try to set up prisoners dilemna as a class.
using namespace std;
int main(){
bot botTrue;
BotTrue(&botTrue);
bot botFalse = BotFalse();
cout<<"Please input a name for botOther\n";
char name[20];
cin>>name;
bot botOther = BotInitialise(name);
BotPrint(botTrue);
BotPrint(botFalse);
BotPrint(botOther);
}
Code:
#ifndef BOTS_H_INCLUDED
#define BOTS_H_INCLUDED
class Bot : public TObject {
public:
struct bot{
int money;
int rank;
char* name;
};
bot botA;
bot BotTrue(){return botA;}
bot BotFalse(){return botA;}
bot BotInitialise(char name[]){return botA;}
int BotRank(bot botA){return 0;}
void BotPrint(bot botA);
};
#endif // BOTS_H_INCLUDED
Code:
#include"bots.h"
bot BotTrue(bot* botA)
{
botA.name = "Truth";
botA.money = 0;
return(0);
}
bot BotFalse()
{
bot botA;
botA.name = "False";
botA.money = 0;
return(botA);
}
bot BotInitialise(char name[])
{
bot botA;
botA.name = &name;
botA.money =0;
return(botA);
}
int BotRank()
{
return(0);
}
void BotPrint(bot botA)
{
cout<<botA.name<<" money = "<<botA.money<<"\n";
cout<<botA.name<<" rank = "<<botA.rank<<"\n";
}
Code:
/home/dan/Desktop/PD/bots.h|3|error: expected class-name before ‘{’ token|
/home/dan/Desktop/PD/bots.cpp|3|error: ‘bot’ does not name a type|
/home/dan/Desktop/PD/bots.cpp|9|error: ‘bot’ does not name a type|
/home/dan/Desktop/PD/bots.cpp|17|error: ‘bot’ does not name a type|
/home/dan/Desktop/PD/bots.cpp|30|error: variable or field ‘BotPrint’ declared void|
/home/dan/Desktop/PD/bots.cpp|30|error: ‘bot’ was not declared in this scope|
||=== Build finished: 6 errors, 0 warnings ===|
How should the syntax be? Why does my program not recognise bot as an object type?
Why can I not have a void method?
Thanks in advance - cohen990