Sign in to follow this  
Nanook

cannot compile.... [c++]

Recommended Posts

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[i].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[i];

  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[i] << '\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


Share this post


Link to post
Share on other sites
Didnt post the error msg.. here it is:


--------------------Configuration: registration - Win32 Debug--------------------
Compiling...
UNIT.CPP
j:\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.CPP
REGIST.CPP
j:\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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
In your class methods have you tried using the keyword 'this' before your private variables. this.name = nam; I'm not familiar enough with the older versions of the Microsoft IDE's to know if they're that picky or not. I had a similar problem once in a different language and putting the scope before the variable fixed it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nanook
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..


dev-cpp isn't a good choice either as it is no longer supported and buggy. Ask your teacher if you can use VS 2005/2008, or if that isn't available I recommend code::blocks.

Share this post


Link to post
Share on other sites
if I was you i would get vs 2008 pro which is free for student or discounted. The software is usually 1000 plus but for you it would be either free or 100 something. Later on you can dive into mfc which is nice to try out before finishing school. You cant work with mfc out side the vs 2008 retail version so express version doesnt have it

Share this post


Link to post
Share on other sites
Looking at your code, you are trying to access a private variable name from a "Unit" object passed into your method.
You will need to either declare that variable as public in your Unit.h file OR create a getter/setter method such as
public variable method

public:
std::string name;

Getter/Setter method

public:
std::string GetName(){return name;}
void SetName(std::string nm){ name = nm;}
private:
std::string name;

The accepted best practice is to use the getter/setter method for each private variable rather than exposing a variable as public.
Hope that helps

Share this post


Link to post
Share on other sites
Quote:
Original post by mudrugger
Looking at your code, you are trying to access a private variable name from a "Unit" object passed into your method.
You will need to either declare that variable as public in your Unit.h file OR create a getter/setter method such as
public variable method

public:
std::string name;

Getter/Setter method

public:
std::string GetName(){return name;}
void SetName(std::string nm){ name = nm;}
private:
std::string name;

The accepted best practice is to use the getter/setter method for each private variable rather than exposing a variable as public.
Hope that helps


That was my first thought too, without even looking at the code, but just by looking at the errors. I'm still learning myself, but I remember in one of the books that I had read about having these kinds of issues and to make sure you understand the difference.

Share this post


Link to post
Share on other sites
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?


1) 6.0 is more than a decade old. In computer terms, that's paleolithic.
2) Current versions are available from Microsoft for free. You should be able to get them through MSDN, even.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joshuad
Quote:
Original post by mudrugger
Looking at your code, you are trying to access a private variable name from a "Unit" object passed into your method....

That was my first thought too, without even looking at the code, but just by looking at the errors.


That's what the 'friend' keyword, as used in unit.h, is for. :)

Share this post


Link to post
Share on other sites
Yeah, I found out about dev-cpp being outdated.. and got code::blocks now.. I'll have to ask my teacher at uni.. maybe I've downloaded the wrong version or something.. but I think I'll stick to code::blocks for now.. got way to much work to get done befor I got time to experiment with different compilers right now :p

Thanks a bunch for your responses! :)

Share this post


Link to post
Share on other sites
As a side Nanook,

You should be able to get a copy of Visual Studio 2008 from microsoft directly as a part of the dreamspark program where, depending on your Uni, you may be eligible to download the Pro versions of VS2008, VS2008, along with std versions of Server 2003 and 2008.

Hit up www.dreamspark.com for a looksee

as far as the code goes...

Is referring to U.name from the your "istream &operator >>" in unit.cpp within the scope of the Unit class? (i see the friends, but i've not had experience with it in this context.)

As its not a "unit::" is it not then trying to access a private variable within the unit class in a public fashion?

just an idea, but maybe try modifying the relevant references and change the "istream& operator>>" to "istream& UNIT::operator>>", and remove the friend references as a trial...

[Edited by - Kalten on March 18, 2009 7:39:03 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this