CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Simple problem

  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Simple problem

    Im very Confused where to start the practical can anyone give me any tips etc?


    class Student {

    private:

    string studentName; // Would this be initialising a student's name and program code to empty strings.??????
    string studentProgramCode;

    public:
    /**
    * Default constructor.
    * Creates an empty Student object by initialising a student's name and
    * program code to empty strings.
    */
    Student(); //Default constructor.???

    /**
    * Constructor.
    * Creates a Student object with the given name and program code.
    */
    Student(const string& name, const string& programCode);

    /**
    * return the name of this student.
    */
    string getName()const;

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Simple problem


  3. #3
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Simple problem

    Hi,

    >Im very Confused where to start the practical can anyone give me any tips etc?
    Just the usual tips - read the question and think about it. If you have a chance to, discuss with the other pupils and get their ideas. If you have any examples from class, have a look at those.

    What you have shown is the first bit of a class declaration, this would usually go in a header file, and tells the compiler what to expect to find in the class.

    Normally the next step would be to write the class definition, which tells the compiler how the class actually works, e.g. how GetName() works, what the constructors initialise etc.

    However, it will depend on what your assignment actually is, that's not possible to tell from your post.

    Alan

  4. #4
    Join Date
    Apr 2009
    Posts
    4

    Re: Simple problem

    Quote Originally Posted by mayo88 View Post
    Im very Confused where to start the practical can anyone give me any tips etc?


    class Student {

    private:

    string studentName; // Would this be initialising a student's name and program code to empty strings.??????
    string studentProgramCode;

    public:
    /**
    * Default constructor.
    * Creates an empty Student object by initialising a student's name and
    * program code to empty strings.
    */
    Student(); //Default constructor.???

    /**
    * Constructor.
    * Creates a Student object with the given name and program code.
    */
    Student(const string& name, const string& programCode);

    /**
    * return the name of this student.
    */
    string getName()const;

    ok so i created another file named Students.cpp.

    Ive started writing it... as shown below:

    #include "Student.h"
    #include <cstdlib>
    #include <iostream>


    Student::Student()
    {
    studentName = "NAME";
    studentProgramCode = "CODE";
    }

    Student::Student(const string& name, const string& programCode)

    Student::string getName()
    {
    return studentName;
    }

    }

    I want to test if its working or not? but i keep getting errors:

    --------------------Configuration: Assignment - Win32 Debug--------------------
    Compiling...
    Student.cpp
    Prac1\Student.cpp(14) : error C2039: 'string' : is not a member of 'Student'
    prac1\student.h(22) : see declaration of 'Student'
    prac1\Student.cpp(14) : error C2146: syntax error : missing ';' before identifier 'string'
    prac1\Student.cpp(14) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    prac1.exe - 3 error(s), 0 warning(s)

    any ideas?

  5. #5
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Simple problem

    Not bad, but this is the line causing the problem:

    Code:
    Student::Student(const string& name, const string& programCode)
    You need to add a function body for that constructor, same as you already did for the default constructor and for getName().

  6. #6
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Wink Re: Simple problem

    Quote Originally Posted by mayo88 View Post
    ok so i created another file named Students.cpp.

    Code:
       Student::Student()
       {
          studentName = "NAME";
          studentProgramCode = "CODE";
       }
    
       Student::Student(const string& name, const string& programCode)
    
       Student::string getName()
       {
          return studentName;
       } 
    
    }
    I want to test if its working or not? but i keep getting errors:

    --------------------Configuration: Assignment - Win32 Debug--------------------
    Compiling...
    Student.cpp
    Prac1\Student.cpp(14) : error C2039: 'string' : is not a member of 'Student'
    prac1\student.h(22) : see declaration of 'Student'


    any ideas?
    See the correct way to define the member function outside the class definition.

    Student::string getName() correct it to ret_type ClassName::MemFunName()

    so string Student::getName()...

    I am also new to c++... Correct me if i am wrong...

  7. #7
    Join Date
    Feb 2009
    Posts
    42

    Re: Simple problem

    No, you're right Looser_007, also indenting your code is good thing, and it will show you where you have another error. Every open code block, procedure that's open with { need to have closed block, procedure etc with }

  8. #8
    Join Date
    Apr 2009
    Posts
    4

    Re: Simple problem

    Quote Originally Posted by alanjhd08 View Post
    Not bad, but this is the line causing the problem:

    Code:
    Student::Student(const string& name, const string& programCode)
    You need to add a function body for that constructor, same as you already did for the default constructor and for getName().

    Student::Student(const string& name, const string& programCode)
    {
    studentName = name;
    studentProgramCode = programCode;
    }
    //Creates a Student object with the given name and program code.
    Is this what you mean???

    Thanks everyone for your replys.

  9. #9
    Join Date
    Apr 2009
    Posts
    4

    Re: Simple problem

    prac1\Student.cpp(19) : error C2511: 'getName' : overloaded member function 'class std::basic_string<char,struct std::char_trai
    ts<char>,class std::allocator<char> > (void)' not found in 'Student'
    prac1\student.h(22) : see declaration of 'Student'
    Error executing cl.exe.

    Assignment.exe - 1 error(s), 0 warning(s)

    Receiving this error now, It cant find Student.h???

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured