Page 3 of 3 FirstFirst 123
Results 21 to 25 of 25
  1. #21
    Join Date
    Jan 2004
    Posts
    76
    Quote Originally Posted by Skirge View Post
    I didn't think you needed the scope operator in the header, just in the cpp?
    acctually the :: (quadpoint ) is the scope operator that ":" is for inheritance
    stuff.

  2. #22
    Join Date
    Jun 2005
    Location
    NY
    Posts
    1,374
    The ':' is for an initializer list. When you derive it calls the base class's constructor(and destructor) so you can use the initializer list to send the base class constructor parameters.

    It would be the same as manually assigning the variables like:
    Assume DerivedClass is derived from BaseClass, and BaseClass has a public(or protected) member variable int nBaseClassParam
    Code:
    DerivedClass::DerivedClass(int nParam1, int nBaseParam)
    {
        nBaseClassParam = nBaseParam;
        nDerivedParam = nParam1;
    }
    is basically the same as:
    Code:
    DerivedClass::DerivedClass(int nParam1, int nBaseParam) : BaseClass(nBaseParam)
    {
        nDerivedParam = nParam1;
    }
    assuming BaseClass has a constructor that has one integer parameter.
    CodeGuru: DLL Tutorial For Beginners by me. Rated 4 1/2 out of 5.

  3. #23
    Join Date
    Mar 2008
    Posts
    27
    Quote Originally Posted by dodlegion View Post
    Heres another set of errors that I dont understand.

    error C2589: '(' : illegal token on right side of '::'
    error C2059: syntax error : '::'
    error C2143: syntax error : missing ';' before '{'
    error C2447: '{' : missing function header (old-style formal list?)


    Code:
    #include <iostream>
    #include "Employee.h"
    #include <string>
    using namespace std;
    
    Employee::Employee(std::string first, std::string last, int salary)
    {
    	firstName = first;
    	lastName = last;
    	Salary = salary;
    }

    well, im still learning c++ but from just looking at it the whole :: looks wrong.

    if you are using "using namespace std" you shouldnt need to use std::string.

    using namespace take the place of the ::, i would try taking out using namespace from the top and see what happens.

    im pretty sure if you declare a string and put a , after each one you dont need to declare each one as a string. (thats not the problem im sure, you can get away with it)

    like

    string first, last, int salary;

    //----
    Employee::Employee(std::string first, std::string last, int salary)

    i think its getting confused since you are calling the function with the :: but u have :: within the () so if you take out the using namespace std or change the :: inside the () to (string first, string last, int salary could fix it.

    hope this helps, im no expert by all means but i think thats whats wrong.
    good luck.

    ~blaze

  4. #24
    Join Date
    Jun 2005
    Location
    NY
    Posts
    1,374
    The '::' is a scope resolution operator. Specifying 'using namespace std' just makes it so you don't have to write out 'std::string'(or anything in the std namespace), but you still can. The '::' is required for the class function definition.
    The compiler knows about the namespace std because of the inclusion of iostream, so it won't confuse the two.

    If the compiler got confused with that kind of stuff, a whole lot of people would be extremely angry, since what would happen if you were to create your own namespace, pass something from it to a class, and you only use one thing from the namespace in the class so there is no need for using the namespace?
    CodeGuru: DLL Tutorial For Beginners by me. Rated 4 1/2 out of 5.

  5. #25
    Join Date
    Nov 2006
    Location
    UK
    Posts
    3,774
    I did a quick test of this making a test function and having namespace std as well as then putting std::string, does not confuse the compiler.
    Delphi !ROCKS!
    Got a question? Read this first!!!
    "You gotta help us, Doc. We've tried nothin' and we're all out of ideas"

Page 3 of 3 FirstFirst 123

Posting Permissions

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