First time library - simple file - does not name a type error

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 --

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
#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);

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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 

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
#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";
}


1
2
3
4
5
6
7
/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
class Bot : public TObject {

And what's a TObject?

Something i found in an example online - http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/class.html

It gives me the "/home/dan/Desktop/PD/bots.h|3|error: expected class-name before ‘{’ token|2" error but without it the other errors are still present - I added this as an attempt at a fix
Last edited on
Well, in C++, if you're going to inherit from an object, that objec type must exist. Your code does not define any object named TObject, so you cannot inherit from it. This is a really important point.

1
2
3
4
5
6
7
class Bot : public TObject {
    public:
struct bot{
 int   money;
   int rank;
  char*  name;
};


This is something of a mess. So you're trying to define a class (which in C++ is the same as a struct, except for default privacy) and then inside it you immediately define another class, with almost the same name?

This is what I think you were aiming for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef BOTS_H_INCLUDED
#define BOTS_H_INCLUDED
class bot {
    public:
   int   money;
   int rank;
  char*  name;

bot BotTrue();
bot BotFalse();
bot BotInitialise(char* name);
int BotRank();
void BotPrint();
};
#endif // BOTS_H_INCLUDED  



bot botA;
Creating an instance of an object in a header is not necessarily a bad idea, but in this code, it seems a bit off, and trying to create this instance of an object in the middle of a class definition makes no sense (it would mean that your bot object contained another bot, but that bot would be a complete bot that contained another bot, but that bot object would also contain another bot etc etc, like Russian dolls to infinity - this is clearly not going to work).

Your functions are almost all defined in the bots.h file, and then you define them again (differently!) in whatever that third file is. This is bad. Define them once. Make sure you know the difference between definition and declaration.

There are lots more problems in this code. Fundamentally, I think you've tried something far too big for your current level of expertise. That you tried to fix it by sticking in : public TObject because you saw that somewhere on the internet is a big clue that you don't understand inheritance. There is no need for inheritance in this class, so just don't do it.

I see that in many of your functions (which, being class functions, happen inside an already existing bot object) you create a whole new bot, which you then alter and then return. This demonstrates a fundamental misunderstanding of what classes are for and how class functions work.

You need to start with a very simple class definition and get that working, and then add pieces one at a time. Here's a start:

1
2
3
4
5
6
7
8
9
class bot{
public:
  int money;

  void BotInitialise()
  {
    money =0;
  }
};



In C++, we tend to not use specific initialisation functions like this, because we have constructor functions. It's worth learning about them too.
Last edited on
Topic archived. No new replies allowed.