I'm getting some errors with my code, & cant seem to find where the problems are.
Basically I'm making a student class that calls out the student id & name.
I hope someone can point me in the righ direction to get this code working.

Thank you

student.cpp:

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

using namespace std;

// cconstructor for your class
Student::Student() 
{
    // code to do any initialization needed for a new object instance
    //myPrivateInteger = 0;
}
Student::Student(int ID, string Name)
{
    ID = newid;
    Name = newname;
}
// destructor for your class
Student::~Student() 
{
// code to do any clean up or checking needed when an
// object is destroyed
// free any memory allocated by this class
}
int Student::getStudentID()
{
    return ID;
}
string Student::getStudentName()
{
    return Name;
}

void Student::setStudentName(string newname) 
{
    Name=newname;
}
void Student::setStudentID(int newid)
{
    ID = newid;
}
void Student::display()
{
    cout << "Name: ">> Name; 
    cout <<   endl;
    cout << "ID: " >> ID; 
    cout << endl;
}

Student.h:

#include <iostream>
#include <string>
using namespace std;

#ifndef STUDENT_H
#define STUDENT_H

class Student
{
    public:
    // constructor
    Student();
    Student(int, string);

    // destructor
    ~Student();

    string getStudentName();
    int getStudentID();

    void setStudentName(string newName);
    void setStudentID(int newID);
    void display();

    private:

    int newid; 
    string newname; 

}; 
#endif

driver.cpp

#include "Student.h"

int main()
{
    Student student;
    student.setStudentID("1234");
    student.setStudentName("tom");

    student.display();

    return 0;
}

the errors are
error C2664: 'Student::setStudentID' : cannot convert parameter 1 from 'const char [8]' to 'int'
error C2065: 'ID' : undeclared identifier
error C2065: 'Name' : undeclared identifier
error C2065: 'Name' : undeclared identifier
error C2065: 'ID' : undeclared identifier
error C2065: 'Name' : undeclared identifier
error C2065: 'ID' : undeclared identifier

> Student::Student(int ID, string Name)
> {
> ID = newid;
> Name = newname;
> }

Shouldn't this maybe be (looks so but I am more C than C++ guy, never did OO with C++):

Student::Student(int newid, string newname)
{
ID = newid;
Name = newname;
}

thanks, but
unfortunately, i'm still getting the same errors

student.setStudentID("1234");

You are giving character instead of number like error message tells you,

i've changed my driver.cpp to look like this

#include "student.h"

int main()
{
    Student student(1234, "Tom");


    student.display();

    return 0;
}

however, i'm still getting the following errors,
error C2065: 'ID' : undeclared identifier
error C2065: 'Name' : undeclared identifier
error C2065: 'Name' : undeclared identifier
error C2065: 'ID' : undeclared identifier
error C2065: 'Name' : undeclared identifier
error C2065: 'ID' : undeclared identifier

I do not know about what could be error there, but one more strange thing is that you declare

private:

int newid; 
string newname; 

Even those are not class variables but parameters to methods. Maybe I just don't know, but looks strange thing to do. Maybe it should be

private:

int ID; 
string Name; 

Edited 4 Years Ago by pyTony

Let's go through this problem by problem.

1) You got completely confused while naming your member variables. As pyTony said above, in student.h:

    int newid;
    string newname;

Should be changed to:

    int ID;
    string Name;

To finish fixing these naming conflicts, change Student::Student(int ID, string Name) to Student::Student(int newid, string newname) in student.cpp.

2) In main(), you are calling student.setStudentID("1234");. When you put quotes around something, you're turning it into a char*. This function is supposed to take an int. Remove the quotation marks on this line and it should work.

3) In Student::display(), some of your chevrons are backwards when using cout. Flip those around.

Edited 4 Years Ago by Tumlee

ah, yes that fixed all my problems.. I guess i messed up on the naming portions.

Thank you for making things clear to me.

This question has already been answered. Start a new discussion instead.