Making a really basic class in C++

Having an issue declaring .h and then calling it in main

Page 1 of 1

6 Replies - 734 Views - Last Post: 22 February 2009 - 03:43 PM Rate Topic: -----

#1 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

Making a really basic class in C++

Posted 22 February 2009 - 03:07 PM

I am trying to build a very basic class in C++ I have a header file
#ifndef time
#define time

class time
{
	int month;		
				
};
#endif 


and i and just trying to cin month and cout month in my main
#include <iostream>
#include "time.h"
using namespace std;

int main()
{
	cin >> time.month;
	cout << time.month << endl;
}


I keep getting this error "error C2228: left of '.month' must have class/struct/union"
what is wrong with this code?

Is This A Good Question/Topic? 0
  • +

Replies To: Making a really basic class in C++

#2 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Making a really basic class in C++

Posted 22 February 2009 - 03:20 PM

Try
class time
{
	   public:
			int month;
};


Was This Post Helpful? 0
  • +
  • -

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Making a really basic class in C++

Posted 22 February 2009 - 03:20 PM

You need to create an instance of the time class:
time my_time;

cin >> my_time.month;



And as cam pointed out.... month must be set to public

Not specifiying: public, private, protected defaults the member to private.

This post has been edited by e_barroga: 22 February 2009 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Making a really basic class in C++

Posted 22 February 2009 - 03:27 PM

Also, when using #ifndef time and #define time, you shouldn't be defining using the same name of your class. Use _TIME or something similar instead.
Was This Post Helpful? 0
  • +
  • -

#5 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

Re: Making a really basic class in C++

Posted 22 February 2009 - 03:31 PM

View Poste_barroga, on 22 Feb, 2009 - 02:20 PM, said:

You need to create an instance of the time class:
time my_time;

cin >> my_time.month;



And as cam pointed out.... month must be set to public

Not specifiying: public, private, protected defaults the member to private.

i have tried the my_time and it still gives me the same error "error C2228: left of '.month' must have class/struct/union"
Was This Post Helpful? 0
  • +
  • -

#6 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Making a really basic class in C++

Posted 22 February 2009 - 03:37 PM

You might want to adjust three things with your source code.

For your defines in your header file, use something other than your class name:
#ifndef TIME_H
#define TIME_H

//Omitted Code

#end /*TIME_H*/



If you want to access class members outside of the class, you must make the member public


class my_class {
  public:
	int a_public_member;
};


Create an instance of your class:
int main () {
  my_class an_instance;
  cin >> an_instance.a_public_member;
  return 0;
}

Was This Post Helpful? 1
  • +
  • -

#7 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 997
  • Joined: 30-July 08

Re: Making a really basic class in C++

Posted 22 February 2009 - 03:43 PM

I don't use names of already existing classes even if the header isn't included (time is in time.h) so I changed it to ttime.
Anyway I did like this:
ttime.h
#ifndef _TTIME_H_
#define _TTIME_H_

class ttime
{
	   public:
			int month;
};

#endif


main.cpp
#include <iostream>
#include "ttime.h"
using namespace std;

int main()
{
	ttime t;
	cin >> t.month;
	cout << t.month;
}


And it compiles and run for me.

This post has been edited by FrozenSnake: 22 February 2009 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1