Thread: error C2061: syntax error : identifier

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    error C2061: syntax error : identifier

    Hello!

    I've recently started with c++ (in fact this is my first project).
    I've got some experience in C# so starting to learn c++ wasn't that hard.
    But now I've met a road block.

    The error started showing when I added my second class inheriting from another class.
    It's seems the error has something to do with my #define's and my #include's but I've been tinkering around with it and I can't find a solution on my own.

    You'll propably see that I've structured everything very weirdly, there were just .cpp files (and the Library.h file) before I started tinkering with this problem. I don't know if that has something to do with the problem.

    Here's all the code:
    http://www.hakucenter.com/random/cpptest01.rar

    Here's the errors:
    Code:
    1>c:\users\haku\documents\visual studio 2008\projects\ccptest02\ccptest02\player.h(34) : error C2061: syntax error : identifier 'Weapon'
    1>c:\users\haku\documents\visual studio 2008\projects\ccptest02\ccptest02\player.h(36) : error C2065: 'wpn' : undeclared identifier
    1>c:\users\haku\documents\visual studio 2008\projects\ccptest02\ccptest02\player.h(37) : error C2065: 'arm' : undeclared identifier
    1>c:\users\haku\documents\visual studio 2008\projects\ccptest02\ccptest02\main.cpp(27) : error C2661: 'Player::Player' : no overloaded function takes 2 arguments
    I'm sorry if i'm wasting your time with a simple problem. Usually I can work things out on my own, but I spent an hour yesterday with it (and I can't put a breakpoint and go through the includes/defines step by step either = / ) without prevailing.

    Thanks in beforehand!
    // Mattias Hakulinen

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your include hiearchy is way wrong, and I suggest you fix it before doing anything else.
    Any header that depends on another should include that header.
    For example, Player.h should include Weapon.h.
    Never rely on a .cpp file including the appropriate headers that a header needs!

    After that, I think you need to stop being so extreme on pointers. Remove them and try optimizing if you see a performance hit.
    And don't use char for strings, use std::string instead. Your current method is unsafe.
    And my last suggestion is to make the classes do actions instead, like Attack, Defend instead of TakeDamage.

    Ah yes, you may also want to learn about references.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Thanks for the reply.

    Yeah I know that my include hierarchy is way off, that is my problem.
    I would very much like to know how I should do it instead, and how to fix my problem.

    As I said before, this is my first project and I'm not really looking for the best method to do everything. I can't go from knowing nothing to knowing the best optimal way to do everything in 8~ hours. This project is only for learning, and I will keep adding/removing stuff until I realize that everything is ****ed up and I have to create a new project ... that's my way of learning.

    Back to the subject at hand; I know adding weapon.h and armor.h to player doesn't solve anything. Actually I knew that they had to be there. I must've tried something different yesterday evening and therefore removing them.

    What about Inheritence ? If class WEAPON, inherits from ITEM, does weapon have to know about item somehow? Like with #include "Item.h" or class Item; ?
    Theese are the things I don't understand, 'who' need 'what' and 'why'.

    About the string thing, I will try to change that. Actually I looked at a source code for a textbase rpg and he had done it that way (with the char[20] names and char pointers).

    Thanks // Mattias
    Last edited by maninboots; 07-02-2009 at 09:22 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should change to the right include hierarchy first, even if it doesn't solve the problem, because you will want this in the end, and if you make fixes before you change to the new hierarchy, you will just have to change it later and potentially break the code.

    Code:
    class Item;
    This is called a forward declaration. It can be used ONLY if you use the type in conjunction with pointers and references, ie:
    Item*
    or
    Item&
    Any other case, you MUST include the header so the compiler can see the class definition (yes, this includes inheritance).

    I don't expect you or anyone else to become a pro overnight, of course, but slowly making suggestions and changes makes everyone learn.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Ok! Thanks alot.
    I will check out a couple of articles about include hierarchy and do some tutorials.
    I'll come back if there's something else on my mind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM