CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2012
    Posts
    3

    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

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: First time using library - Error with struct declaration

    Quote Originally Posted by cohen990 View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: First time using library - Error with struct declaration

    Quote Originally Posted by D_Drmmr View Post
    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jul 2012
    Posts
    3

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured