|
-
April 3rd, 2009, 02:02 AM
#1
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;
-
April 3rd, 2009, 02:11 AM
#2
-
April 3rd, 2009, 02:15 AM
#3
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
-
April 3rd, 2009, 03:21 AM
#4
Re: Simple problem
 Originally Posted by mayo88
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?
-
April 3rd, 2009, 03:47 AM
#5
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().
-
April 3rd, 2009, 03:47 AM
#6
Re: Simple problem
 Originally Posted by mayo88
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...
-
April 3rd, 2009, 03:59 AM
#7
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 }
-
April 3rd, 2009, 04:15 AM
#8
Re: Simple problem
 Originally Posted by alanjhd08
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.
-
April 3rd, 2009, 04:17 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|