We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Sntax Error

Medium Priority
450 Views
Last Modified: 2013-12-16
Hi guys: Can any one please help me in that error. Thanks.

class Employee
{
      public:
             string name;
             double salary;
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
}


Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Documents and Settings\Kathy\My Documents\test.cpp" -o "C:\Documents and Settings\Kathy\My Documents\test.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib"
C:\Documents and Settings\Kathy\My Documents\test.cpp:5: error: `string' does not name a type

C:\Documents and Settings\Kathy\My Documents\test.cpp:11: error: definition of implicitly-declared `Employee::Employee()'
C:\Documents and Settings\Kathy\My Documents\test.cpp:11: error: declaration of `Employee::Employee()' throws different exceptions
C:\Documents and Settings\Kathy\My Documents\test.cpp:3: error: than previous declaration `Employee::Employee() throw ()'
C:\Documents and Settings\Kathy\My Documents\test.cpp: In constructor `Employee::Employee()':
C:\Documents and Settings\Kathy\My Documents\test.cpp:12: error: `name' undeclared (first use this function)
C:\Documents and Settings\Kathy\My Documents\test.cpp:12: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\Documents and Settings\Kathy\My Documents\test.cpp: At global scope:
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: expected `)' before "n"
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: expected `,' or `;' before "n"

Execution terminated
Comment
Watch Question

is this c++ or c#?

I think it should be: (sorry if not 100% correct)
c#
class Employee
{
      public:
             string name;
             double salary;

      Employee()
      {
          name = "";
          salary = 0.0;
      }

      Employee(string n, double s)
      {
        name = n;
        salary = s;
      }
}

Open in new window


c++
class Employee
{
      public string name;
      public double salary;

      public Employee();
      public Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
}

Open in new window

Author

Commented:
Thanks. Its a C++

class Employee
{
      public string name;
      public double salary;

      public Employee();
      public Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
}



Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Documents and Settings\Kathy\My Documents\test.cpp" -o "C:\Documents and Settings\Kathy\My Documents\test.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib"
C:\Documents and Settings\Kathy\My Documents\test.cpp:3: error: expected `:' before "string"
C:\Documents and Settings\Kathy\My Documents\test.cpp:3: error: `string' does not name a type
C:\Documents and Settings\Kathy\My Documents\test.cpp:4: error: expected `:' before "double"
C:\Documents and Settings\Kathy\My Documents\test.cpp:6: error: expected `:' before "Employee"

C:\Documents and Settings\Kathy\My Documents\test.cpp:7: error: expected `:' before "Employee"

C:\Documents and Settings\Kathy\My Documents\test.cpp:7: error: expected `)' before "n"

C:\Documents and Settings\Kathy\My Documents\test.cpp: In constructor `Employee::Employee()':
C:\Documents and Settings\Kathy\My Documents\test.cpp:12: error: `name' undeclared (first use this function)
C:\Documents and Settings\Kathy\My Documents\test.cpp:12: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\Documents and Settings\Kathy\My Documents\test.cpp: At global scope:
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: expected `)' before "n"
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: expected `,' or `;' before "n"

Execution terminated
sorry i did the class declaration
 
class Employee
{
      public:
            string name;
            double salary;

            Employee();
            Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
}

Open in new window

i think i have gotten a little bit confused.

are you including a header for the type "string"?

Author

Commented:
Thanks. It still give error on that line
string name;

C:\Documents and Settings\Kathy\My Documents\test.cpp: At global scope:
C:\Documents and Settings\Kathy\My Documents\test.cpp:17: error: expected `)' before "n"
C:\Documents and Settings\Kathy\My Documents\test.cpp:17: error: expected `,' or `;' before "n"

Execution terminated


class Employee
{
      public:
            string name;
            double salary;

            Employee();
            Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
is that the whole cpp file?

Author

Commented:
#include<iostream>
class Employee
{
      public:
            string name;
            double salary;

            Employee();
            Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;

Author

Commented:
Yes.
#include<iostream>

class Employee
{
      public:
            std::string name;
            double salary;

            Employee();
            Employee(std::string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(std::string n, double s)
{
  name = n;
  salary = s;
}

Open in new window

Author

Commented:
im sorry. I think main is missing in that. I am just trying to play with the object oriented programming.
yer you need a main

also you need to add
std:: before and string declarations
eg:
std::string name;

Author

Commented:
Can you please tell me the changes in the code



class Employee
{
      public:
            string name;
            double salary;

            Employee();
            Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
}

int main()
#include<iostream>
{
      system("pause");
      return 0;
}
from you original?

#include<iostream> should be at the top of the file

Author

Commented:
Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Documents and Settings\Kathy\My Documents\test.cpp" -o "C:\Documents and Settings\Kathy\My Documents\test.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib"
C:\Documents and Settings\Kathy\My Documents\test.cpp:5: error: `string' does not name a type

C:\Documents and Settings\Kathy\My Documents\test.cpp:9: error: expected `)' before "n"

C:\Documents and Settings\Kathy\My Documents\test.cpp: In constructor `Employee::Employee()':
C:\Documents and Settings\Kathy\My Documents\test.cpp:14: error: `name' undeclared (first use this function)
C:\Documents and Settings\Kathy\My Documents\test.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\Documents and Settings\Kathy\My Documents\test.cpp: At global scope:
C:\Documents and Settings\Kathy\My Documents\test.cpp:18: error: expected `)' before "n"
C:\Documents and Settings\Kathy\My Documents\test.cpp:18: error: expected `,' or `;' before "n"

Execution terminated



#include<iostream>
class Employee
{
      public:
            string name;
            double salary;

            Employee();
            Employee(string n, double s);
};

Employee::Employee()
{
    name = "";
    salary = 0.0;
}

Employee::Employee(string n, double s)
{
  name = n;
  salary = s;
}

int main()
{
      system("pause");
      return 0;
}
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.