Unresolved Token Errors in Class

Mar 26, 2010 at 2:44am
I am just learning C++, and am having a hard time with creating a class definition. Can you please help me understand what I am doing wrong in the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Student.h
#pragma once
ref class Student
{
private:
  int id;
  double gpa;
  int credits;
  Student() {};
public:
  Student(int, double, int);
  void setGPA(double);
  void setCredits(int);
  int getID() {return id;}
  double getGPA() {return gpa;}
  int getCredits() {return credits;}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
// Student.cpp
#include "StdAfx.h"
#include "Student.h"
Student::Student(int idNum, double stuGPA, int stuCredits)
{
  id = idNum;
  setGPA(stuGPA);
  setCredits(stuCredits);
}
void Student::setGPA(double stuGPA)
{if (stuGPA>=0 && stuGPA<=4.0) gpa=stuGPA;}
void Student::setCredits(int stuCredits)
{if (stuCredits>=0) credits=stuCredits;}


When I try to compile the program, I receive the following errors
1
2
3
myProgram.obj : error LNK2020: unresolved token (06000007) Student::.ctor
myProgram.obj : error LNK2020: unresolved token (06000009) Student::setGPA
myProgram.obj : error LNK2020: unresolved token (0600000A) Student::setCredits


Thanks in advance,
Scott
Mar 26, 2010 at 3:22am
i dont know why ur compiler return that error..when i compile ur program the error is different..
student.h(3) : error C2143: syntax error : missing ';' before '<class-head>'
student.h(3) : error C2501: 'ref' : missing storage-class or type specifiers
student.h(3) : fatal error C1004: unexpected end of file found

this is what i do to solve

#pragma once

class Student
{

private:
int id;
double gpa;
int credits;
Student() {};

public:
Student(int, double, int);
void setGPA(double);
void setCredits(int);
int getID() {return id;}
double getGPA() {return gpa;}
int getCredits() {return credits;}
};



#include "StdAfx.h"
#include "Student.h"

Student::Student(int idNum, double stuGPA, int stuCredits)
{
id = idNum;
setGPA(stuGPA);
setCredits(stuCredits);
}

void Student::setGPA(double stuGPA)
{
if (stuGPA>=0 && stuGPA<=4.0)
gpa=stuGPA;
}

void Student::setCredits(int stuCredits)
{
if (stuCredits>=0)
credits=stuCredits;
}
Mar 26, 2010 at 3:45am
EAStudent,
When I tried your code, I received the following error
 
myProgram\Student.h(4) : error C2814: 'myProgram::Form1::Student' : a native type cannot be nested within a managed type 'myProgram::Form1'


For the form, I have some basic controls added, but no logic on anything yet (except for adding #include "Student.h" to Form1.h) as I wanted to make sure that the class compiled properly first.

I dont' know if it makes a difference, but here is some additional information about my environment.
OS: Windows Vista
Software: Microsoft Visual C++ 2008
Microsoft .NET Framework version 3.5 SP1
Last edited on Mar 26, 2010 at 3:47am
Mar 27, 2010 at 4:09am
I tried a different compiler and had similar errors to what EAStudent listed, and was able to get it to compile by removing the ref from before class Student in Student.h.

Now I am really confused as it seems my original logic was correct, and I am wondering if there is some issue with Microsoft Visual C++, or a setting that was incorrect.

Does anyone have any suggestions on what I could try?

Thanks,
Scott
Topic archived. No new replies allowed.