cannot compile.... [c++]

Started by
20 comments, last by Kalten 15 years, 1 month ago
Im doing some labs at school and cant compile my code at home.. it workes at uni but not at home.. :/ Im using visual studio 6.0 (c++).. the comments might be abit off cause we got some code from the tutor and was told to change it.. there are 5 files:

// MAIN.CPP - Case Study, Student Registration

// Count the number of courses taken by the student, calculate total credits
// author KRI
// 

#include <iostream>
#include <fstream>
#include "unit.h"
#include "regist.h"  // Registration class declaration

using namespace std;

// Main program:
// Open an input file stream, read a Registration object,
// including its list of courses. Redisplay all information,
// and calculate the total credits taken. Write the results
// to a file stream.

int main()
{
  ifstream infile( "rinput.txt" );
  if( !infile ) return -1;

  Registration R;
  infile >> R;

  ofstream ofile( "routput.txt" );

  ofile << R
    << "Number of courses = " << R.GetCount() << '\n'
    << "Total credits     = " << R.GetCredits() << '\n';

  // Declare and initialize a Unit, and modify
  // its credits.

  Unit aUnit( "Data Structures and Abstractions", "ICT209", 4 );
  aUnit.SetCredits( 5 );
  cout << aUnit << endl;

  return 0;
}


// REGIST.CPP - Registration class implementation.

#include "regist.h"


Registration::Registration()
{
  count = 0;
}

unsigned Registration::GetCredits() const
{
  unsigned sum = 0;
  for(unsigned i = 0; i < count; i++)
    sum += units.GetCredits();

  return sum;
}

istream & operator >>( istream & input, Registration & R )
{
  input >> R.studentId >> R.semester >> R.count;

  for(unsigned i = 0; i < R.count; i++)
    input >> R.units;

  return input;
}

ostream & operator <<( ostream & os, const Registration & R )
{
  os << "Student ID: " << R.studentId << '\n'
     << "Semester:   " << R.semester << '\n';

  for(unsigned i = 0; i < R.count; i++)
    os << R.units << '\n';

  return os;
}



// UNIT.CPP - Unit class implementation

#include "unit.h"

Unit::Unit( const string &nam, const string &id, unsigned cred )
{
  name = nam;
  uId = id;
  credits = cred;
}

istream & operator >>( istream & input, Unit & U )
{
  input >> U.name >> U.uId >> U.credits;
  return input;
}

ostream & operator <<( ostream & os, const Unit & U )
{
  os << "  Unit:    " << U.name << '\n'
     << "  Id: "	  << U.uId << '\n'
     << "  Credits: " << U.credits << '\n';
  return os;
}



// REGIST.H - Registration class definition
// author KRI
// modified smr

#ifndef REGIST_H
#define REGIST_H


#include <iostream>
#include "unit.h"

using namespace std;

const unsigned MaxUnits = 10;

class Registration {
public:
  Registration();

  unsigned GetCredits() const;
  unsigned GetCount() const;

  friend ostream & operator <<( ostream & os, const Registration & R);
  friend istream & operator >>( istream & input, Registration & R );

private:
  long studentId;             // student ID number
  unsigned semester;          // semester year, number
  unsigned count;             // number of units
  Unit units[MaxUnits];		  // array of units
};

inline unsigned Registration::GetCount() const
{
  return count;
}

#endif




// UNIT.H - Course class defintion
// author KRI
// modified smr

#ifndef UNIT_H
#define UNIT_H

#include <iostream>
#include <string>

using namespace std;

class Unit {
public:
  Unit(){};
  Unit( const string &nam, const string &id, unsigned cred );
  // Construct a course from a name, section letter,
  // and number of credits.

  unsigned GetCredits() const;
  // Get the number of credits.

  void SetCredits( unsigned cred );
  // Set the number of credits.

  friend ostream & operator <<( ostream & os, const Unit & U );
  friend istream & operator >>( istream & input, Unit & U);

private:
  string name;
  string uId;
  int  credits;   // number of credits
};


inline unsigned Unit::GetCredits() const
{
  return credits;
}

inline void Unit::SetCredits( unsigned cred )
{
  credits = cred;
}

#endif


Advertisement
Didnt post the error msg.. here it is:

--------------------Configuration: registration - Win32 Debug--------------------Compiling...UNIT.CPPj:\ict209\week3\unit\unit.cpp(14) : error C2248: 'name' : cannot access private member declared in class 'Unit'        j:\ict209\week3\unit\unit.h(30) : see declaration of 'name'j:\ict209\week3\unit\unit.cpp(14) : error C2248: 'uId' : cannot access private member declared in class 'Unit'        j:\ict209\week3\unit\unit.h(31) : see declaration of 'uId'j:\ict209\week3\unit\unit.cpp(14) : error C2248: 'credits' : cannot access private member declared in class 'Unit'        j:\ict209\week3\unit\unit.h(32) : see declaration of 'credits'j:\ict209\week3\unit\unit.cpp(20) : error C2248: 'name' : cannot access private member declared in class 'Unit'        j:\ict209\week3\unit\unit.h(30) : see declaration of 'name'j:\ict209\week3\unit\unit.cpp(21) : error C2248: 'uId' : cannot access private member declared in class 'Unit'        j:\ict209\week3\unit\unit.h(31) : see declaration of 'uId'j:\ict209\week3\unit\unit.cpp(22) : error C2248: 'credits' : cannot access private member declared in class 'Unit'        j:\ict209\week3\unit\unit.h(32) : see declaration of 'credits'MAIN.CPPREGIST.CPPj:\ict209\week3\unit\regist.cpp(22) : error C2248: 'studentId' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(27) : see declaration of 'studentId'j:\ict209\week3\unit\regist.cpp(22) : error C2248: 'semester' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(28) : see declaration of 'semester'j:\ict209\week3\unit\regist.cpp(22) : error C2248: 'count' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(29) : see declaration of 'count'j:\ict209\week3\unit\regist.cpp(24) : error C2248: 'count' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(29) : see declaration of 'count'j:\ict209\week3\unit\regist.cpp(25) : error C2248: 'units' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(30) : see declaration of 'units'j:\ict209\week3\unit\regist.cpp(32) : error C2248: 'studentId' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(27) : see declaration of 'studentId'j:\ict209\week3\unit\regist.cpp(33) : error C2248: 'semester' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(28) : see declaration of 'semester'j:\ict209\week3\unit\regist.cpp(35) : error C2248: 'count' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(29) : see declaration of 'count'j:\ict209\week3\unit\regist.cpp(36) : error C2248: 'units' : cannot access private member declared in class 'Registration'        j:\ict209\week3\unit\regist.h(30) : see declaration of 'units'Error executing cl.exe.registration.exe - 15 error(s), 0 warning(s)
edit: ups didn't read the whole code. At first glance it looks okay to me. Did you compile without warning with a more recent Visual Studio version?
------------------------------------I always enjoy being rated up by you ...
hehe thought so :) I tried to compile the same project in vs 2005 and it worked there.. could there be a setting in 6.0? we're using 6.0 at uni so I would prefere to use that at home too..
Then I would think that is is some VC6 problem with respect to STL, operator overloading and friend functions. You could try to change the classes not to use friends but to define their own overloaded operator << (as I first thought it should be) or add accessor methods to get the value of the private members fro writing them to the stream.
------------------------------------I always enjoy being rated up by you ...
Quote:Original post by Nanook
hehe thought so :) I tried to compile the same project in vs 2005 and it worked there.. could there be a setting in 6.0? we're using 6.0 at uni so I would prefere to use that at home too..


Any chance you're using VC++ 6.0 IDE with a more recent compiler? It sounds strange to me that you're using such an old (and not standard compliant) compiler... well, It sounds stange to me that you're using VC in the university in the first place :-)
We got access to msdna through uni and I downloaded vs 6.0 from there so it might be that it got a more recent compiler implemented?
Quote:Original post by Nanook
hehe thought so :) I tried to compile the same project in vs 2005 and it worked there.. could there be a setting in 6.0? we're using 6.0 at uni so I would prefere to use that at home too..

I'm wondering what kind of university forces students to use VS6 :-/ The compiler is not near to be C++ standard compliant; there are actually a whole bunch of threads about the topic here at the forums.

Might be worth trying to convince your teachers that they should switch to a modern compiler. MSVC2008 Express edition is free of charge, they could also consider using Code::Blocks in combination with a recent gcc/mingw version. Almost everything is better than the VS6 C++ compiler.
-----PARPG - open source isometric 2d old school RPG still looking for interested contributors
Quote:Original post by Nanook
We got access to msdna through uni and I downloaded vs 6.0 from there so it might be that it got a more recent compiler implemented?


Don't know, but I really suggest you to stay away from the VC++ 6 compiler, because it is very old. C++ has been standardized since, and new technologies appeared. VS2008 is a far better choice, or NetBeans for C++. VS2008 let's you also build .Net programs, wich could be a nice thing.
decided to use dev-cpp instead.. we can use that aswell.. I guess we could use another version of vs too.. I'll ask my teacher why we use it at all..

This topic is closed to new replies.

Advertisement