C++ code need help to fix

This is a discussion on C++ code need help to fix within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am newcomer of C++. I got two tiny nasty C++ code to fix, but I can't fix ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    Question C++ code need help to fix

    Hi all, I am newcomer of C++. I got two tiny nasty C++ code to fix, but I can't fix all the syntax errors. Could anybody fix that for me please?

    Thanks in advance!

    Codes as below:
    Code:
    #include <Vector>
    using namespace std;
    class nasty
    {
    public:
    int x(int h)
    {
    return h+1;
    }
    
    vector<double> v;
    int f (rabbit)
    {
    int *a = new int();
    int b = 3;
    *a = 3;
    int &c = *a;
    string s;
    nasty blogs = new nasty;
    b = blogs.x(3);
    }
    }
    and the other one is:
    Code:
    #include <string>
    class cls
    {
    public int g(int h)
    {
    return h+1;
    }
    }
    
    int f (alice)
    {
    int *a = new int();
    int b = 3;
    a = 3;
    int &c = *a;
    string s = "Hello";
    cls cc = new cls();
    b = cc.g(3);
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If syntax errors were so easy to fix, the compilers could do it automatically. The reason compilers don't automatically correct errors is because there's no way for the compiler to understand what the heck you're trying to write since it's not valid C++.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > int f (rabbit)
    What type of rabbit ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    8
    I know the type is missed in int f (rabbit), but I am not sure what type I should add. Can anyone help?

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    88
    Well, since you don't actually use rabbit in that function, I'm thinking you don't need it in the first place. Also, you need semicolons after the end braces of classes and in-class method definitions.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    8
    Thanks for pointing that out. I fixed the code as below:
    Code:
    #include <string>
    class cls
    {
    	public int g(int h)
    	{
    		return h+1;
    	}
    };
    
    int f ()
    {
    	int *a = new int();
    	int b = 3;
    	a = &b;
    	int &c = *a;
    	char s[10] = "Hello";
    	cls cc;
    	b = cc.g(3);
    }
    but the compiler said "nasty.cpp:4: error: expected `:' before "int" ". When I change line 4 to public :int g(int h), it compiled. Why?

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    88
    >When I change line 4 to public :int g(int h), it compiled. Why?

    That's just the rules of the game. It'd probably make more sense if you wrote it like this:
    Code:
    class cls
    {
    public:
    
    	int g(int h)
    	{
    		return h+1;
    	}
    };

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by McReal View Post
    but the compiler said "nasty.cpp:4: error: expected `:' before "int" ". When I change line 4 to public :int g(int h), it compiled. Why?
    Because c++ syntax requires that.
    Access specifiers don't apply to single functions or member variables in c++. They apply to to everything that follows until another access specifier is parsed.
    Kurt

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    8
    Ahaaa, now I got it. Thanks all for both quick replies and kindness!!!

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    McReal, I am really curious to know whether you get this code from a classmate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to fix code
    By anik18 in forum C Programming
    Replies: 19
    Last Post: 05-29-2009, 07:06 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Replies: 1
    Last Post: 05-26-2006, 09:13 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21