error C2061: syntax error : identifier 'string'
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: error C2061: syntax error : identifier 'string'

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    33

    error C2061: syntax error : identifier 'string'

    Hi,

    I've just recently started to learn C++, and I'm encountering some errors I can't seem to figure out.

    InventoryItem.h:
    Code:
    #pragma once
    
    class InventoryItem
    {
    public:
    	InventoryItem(string name, int amount);
    	~InventoryItem(void);
    	string getName(void);
    	int getAmount(void);
    private:
    	string name;
    	int amount;
    };
    InventoryItem.cpp:
    Code:
    #include "stdafx.h"
    #include "InventoryItem.h"
    
    
    InventoryItem::InventoryItem(string name, int amount) : name(name), amount(amount) {}
    
    
    InventoryItem::~InventoryItem(void) {}
    stdafx.h:
    Code:
    #pragma once
    
    #include "targetver.h"
    #include "InventoryItem.h"
    
    #include <stdio.h>
    #include <tchar.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    Errors:

    Code:
    1>d:\c++\consoleapplication\consoleapplication\inventoryitem.h(6): error C2061: syntax error : identifier 'string'
    1>d:\c++\consoleapplication\consoleapplication\inventoryitem.h(8): error C2146: syntax error : missing ';' before identifier 'getName'
    1>d:\c++\consoleapplication\consoleapplication\inventoryitem.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\c++\consoleapplication\consoleapplication\inventoryitem.h(8): warning C4183: 'getName': missing return type; assumed to be a member function returning 'int'
    1>d:\c++\consoleapplication\consoleapplication\inventoryitem.h(11): error C2146: syntax error : missing ';' before identifier 'name'
    1>d:\c++\consoleapplication\consoleapplication\inventoryitem.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: error C2061: syntax error : identifier 'string'

    You should #include <string> in InventoryItem.h and fully qualify string as std::string there.

    I suggest that you don't use pre-compiled headers just yet, i.e., get rid of stdafx.h and disable pre-compiled headers. Later, when you are involved in creating much larger programs, the feature might become useful, but for now it is better to know which header is for what by practicing it. Anyway, you should not have a using directive at file scope in a header file, so get rid of the using namespace std; or move it to InventoryItem.cpp, after including the header(s).

    Also, I suggest that you avoid #pragma once, or use it in addition to the more conventional #define header inclusion guards since #pragma once is not guaranteed to always be available, being fundamentally non-standard as a pragma.
    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

  3. #3
    Join Date
    Apr 2013
    Posts
    33

    Re: error C2061: syntax error : identifier 'string'

    Including string in InventoryItem.h indeed solved the errors.

    As you suggested I stopped using pre-compiled headers, and removed using namespace std from all the headers I am using.
    As for #pragma once, I'm not sure how I should approach changing it.

    My program now runs with no errors but for some reason cout doesn't produce any output.



    InventoryItem.h

    Code:
    #pragma once
    #include <string>
    
    class InventoryItem
    {
    public:
    	InventoryItem(std::string name, int amount);
    	~InventoryItem(void);
    	std::string getName(void);
    	int getAmount(void);
    private:
    	std::string name;
    	int amount;
    };
    InventoryItem.cpp

    Code:
    #include "InventoryItem.h"
    #include <string>
    
    InventoryItem::InventoryItem(std::string name, int amount) : name(name), amount(amount) {}
    
    
    InventoryItem::~InventoryItem(void) {}
    
    
    std::string InventoryItem::getName(void)
    {
    	return name;
    }
    main.cpp

    Code:
    #include "InventoryItem.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    	InventoryItem i ("Sword", 1);
    	
    	cout << "Name: " << i.getName() << endl;
    
    	system("pause");
    
    	return 0;
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: error C2061: syntax error : identifier 'string'

    Quote Originally Posted by royibernthal View Post
    As for #pragma once, I'm not sure how I should approach changing it.
    Look up what include guards are.

    Please see below:
    Code:
    #ifndef INVENTORYITEM_H
    #define INVENTORYITEM_H
    #include <string>
    
    class InventoryItem
    {
        public:
    	InventoryItem(const std::string& theName, int theAmount);
    	~InventoryItem();
    	std::string getName() const;
    	int getAmount() const;
    private:
    	std::string name;
    	int amount;
    };
    #endif
    Note the use of include guards. In addition, functions that do not change the members should be declared as const, and parameters such as strings should be passed by const reference, not by value.

    Second, don't name your parameters the same as your member variables. That just leads to confusing code.
    Code:
    #include "InventoryItem.h"
    InventoryItem::InventoryItem(const std::string& theName, int theAmount) : name(theName), amount(theAmount) {}
    
    InventoryItem::~InventoryItem() {}
    
    std::string InventoryItem::getName() const
    { return name;}
    Code:
    #include "InventoryItem.h"
    #include <iostream>
    using namespace std;
    
    int main() 
    {
    	InventoryItem inv("Sword", 1);
    	cout << "Name: " << inv.getName() << endl;
    	return 0;
    }
    Finally, you should #include what the source file suggests that you #include. There is no usage of std::string in main.cpp, but you #included it -- there was no need to #include <string> in main.cpp. The InventoryItem.h file already takes care of what needed to be included for InventoryItem.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2013
    Posts
    33

    Re: error C2061: syntax error : identifier 'string'

    I understand, I've learned alot from your reply.

    Is it very wrong to give parameters the same names as my member variables? Usually I used to do it just like you said, i.e parameter theAmount for memeber amount. I just thought it might produce nicer code hints while allowing me to work with clean member names.

    I've read about the include guards and I now understand it, however I googled the issue and most people seem to favor #pragma once over include guards, less code to write, possibly faster and avoids possible future name conflicts. Even though it is technically non standard, from what I understand it is supported by any modern compiler. Correct me if I'm wrong I'm pretty much quoting what I've read.

    Regardless, I still see no output.

    Thanks.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,391

    Re: error C2061: syntax error : identifier 'string'

    Regardless, I still see no output.
    What debugging have you done with the debugger? What happens when you step through the program using the debugger? Has the code actually compiled and linked properly to produce an executable program? To make sure you are producing a working program, try this

    Code:
    #include "InventoryItem.h"
    #include <iostream>
    using namespace std;
    
    int main() 
    {
    	//InventoryItem inv("Sword", 1);
    	//cout << "Name: " << inv.getName() << endl;
            cout << "hello world!" << endl;
    	return 0;
    }
    If you still see no output then you have a whole different problem!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2013
    Posts
    33

    Re: error C2061: syntax error : identifier 'string'

    I'm working with a console application. The code is compiled and the black console pops up. I just see no output there.

    I did however notice this, which might be related to my problem:
    Name:  Untitled.jpg
Views: 1374
Size:  89.6 KB
    Last edited by royibernthal; April 3rd, 2013 at 06:53 AM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: error C2061: syntax error : identifier 'string'

    Quote Originally Posted by royibernthal View Post
    I'm working with a console application. The code is compiled and the black console pops up. I just see no output there.
    Look at that little yellow arrow. That means that you haven't executed your main() function yet, so of course you won't see any output. The debugger is waiting for you to "get started".

    Hit F10, then do that several more times, and watch the arrow move.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,391

    Re: error C2061: syntax error : identifier 'string'

    Sorry, your screen shot is completely unreadable. What is it saying?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 2013
    Posts
    33

    Re: error C2061: syntax error : identifier 'string'


  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,286

    Re: error C2061: syntax error : identifier 'string'

    Quote Originally Posted by royibernthal
    Is it very wrong to give parameters the same names as my member variables?
    It is not wrong, but it increases the potential for confusion. In this case I consider them fine since they are only used in the constructor to initialise the member variables, but if you used them in the body of the constructor or some other member function, then I would agree with Paul McKenzie.

    Quote Originally Posted by royibernthal
    I've read about the include guards and I now understand it, however I googled the issue and most people seem to favor #pragma once over include guards, less code to write, possibly faster and avoids possible future name conflicts. Even though it is technically non standard, from what I understand it is supported by any modern compiler. Correct me if I'm wrong I'm pretty much quoting what I've read.
    I doubt that your "most people" conclusion is accurate. There are pros and cons involved here. The main disadvantage for conventional include guards is that you have to invent a unique name. The speed issue is generally a non-issue as long as the include guard is the first thing in the file, and "less code to write" is somewhat bogus since we are talking about three lines. The main advantage of #pragma once is that it avoids the unique name problem, with the disadvantage of being non-standard and having no real certainty that the compiler implements it correctly since it may have to deal with file paths across differing file systems and such.
    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

  12. #12
    Join Date
    Apr 2013
    Posts
    33

    Re: error C2061: syntax error : identifier 'string'

    Yes I agree with you, I only intend to use it in order to initialize the member variables I have no intention of causing further confusion in the rest of my code.
    If I do end up using the properties in the constructor, is adding a "_" prefix a conventional way? I've been adding a "the" and "my" prefix for quite a while now and I'm trying to find a better way of doing it.

    Okay, so if I understand correctly the include guards are the safe way to go.

  13. #13
    Join Date
    Apr 2013
    Posts
    33

    Re: error C2061: syntax error : identifier 'string'

    You are right, but I don't understand. Why isn't it all executed automatically? I doubt it's ideal for me to have to press F10 many times every time I test my program.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,427

    Re: error C2061: syntax error : identifier 'string'

    Quote Originally Posted by royibernthal View Post
    You are right, but I don't understand. Why isn't it all executed automatically? I doubt it's ideal for me to have to press F10 many times every time I test my program.
    That is exactly what the debugger is for, and that is to run your program a step at a time. How are you going to figure out where a bug is located or how your program flows if you aren't able to control the "speed" of your program?

    That arrow is telling you the next line that will be executed. What if your program were 10,000 lines long and contained 1,000 functions? What good would it be to run a program at full speed if there is a bug in function X or on line 278? How would you tell the program to "stop here and let me look what is going on"?

    The debugger allows you to run your program a step at a time, inspect variables, look at the flow of the program, set breakpoints, etc.

    Regards,

    Paul McKenzie

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: error C2061: syntax error : identifier 'string'

    You could set one or more break points and then press F5 to jump from one point to the next.
    Or just run your code without debugging!
    Victor Nijegorodov

Page 1 of 3 123 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center