First time using library - Error with struct declaration
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
Re: First time using library - Error with struct declaration
bot is a substructure of Bot. So you need to use it as Bot::bot in main.cpp.
Your methods have no class specification in them, if you are not implemented inline, you have to tell the compiler what class each function belongs to.
Code:
bot BotTrue(bot* botA)...
should be
Code:
bot Bot::BotTrue(bot* botA)...
Also, TObject doesn't appear to be declared anywhere.
Re: First time using library - Error with struct declaration
Quote:
Originally Posted by
cohen990
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?
Which compiler are you using? The error that is given is not particularly helpful. Looking at the place where the error occurs, it seems to me that the real problem is that TObject is an undeclared identifier. You'll need to include the correct header in bot.h.
Re: First time using library - Error with struct declaration
Quote:
Originally Posted by
D_Drmmr
Which compiler are you using?
If a class starts with T it's very likely to be a Borland C++ Builder class. As far as I remember TObject should be that but I don't remember what header that is missing. :o
Re: First time using library - Error with struct declaration
Hey, sorry this problem has been solved (rather rudely) on a different forum!
My questions about void methods remains unanswered though (the problem was I was using the headers incorrectly and didn't understand c++ classes apprently).
I solved the problem by removing TObject and bots.cpp and rewriting the program completely - though I would still like to know why I cannot make a void method.
Kind regards - Dan Cohen
Re: First time using library - Error with struct declaration
Quote:
Originally Posted by cohen990
I would still like to know why I cannot make a void method.
You can indeed have a member function that has a void return type, so the problem is probably specific to some mistake that you made.