11 Replies - 314 Views - Last Post: 25 July 2011 - 01:22 PM Rate Topic: -----

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 5
  • View blog
  • Posts: 396
  • Joined: 05-February 10

Classes issue

Posted 24 July 2011 - 07:33 PM

I have a main.cpp and a calculator.cpp and a calculator.h, i want to create a Calculator variable in my main so i can its functions. I'v looked at tutorial, videos, google, etc... but still can't figure out this simple task.

main.cpp
#include <iostream>
#include "Calculator.h"

class Calculator var;

int main()
{
using namespace std;
var.myFunc();    
cout << endl;
system("pause");
}



Calculator.cpp
#include "Calculator.h"
using namespace std;

void Calculator::myFunc() {
     cout << "blah";
}



Calculator.h
#ifndef Calculator_H
#define Calculator_H

class Calculator{
      public:
      void myFunc();
}

#endif



My errors are:
- two or more data types in declaration of `var'
- [Build Error] [Main.o] Error 1


Is This A Good Question/Topic? 0
  • +

Replies To: Classes issue

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 604
  • View blog
  • Posts: 1,866
  • Joined: 02-August 09

Re: Classes issue

Posted 24 July 2011 - 07:47 PM

It seems that your compiler doesn't like the class keyword in your declaration of var, although it shouldn't be an issue. What compiler are you using?
Was This Post Helpful? 1
  • +
  • -

#3 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 5
  • View blog
  • Posts: 396
  • Joined: 05-February 10

Re: Classes issue

Posted 24 July 2011 - 07:51 PM

Dev c++
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 604
  • View blog
  • Posts: 1,866
  • Joined: 02-August 09

Re: Classes issue

Posted 24 July 2011 - 08:04 PM

Do you get an error without the "class" keyword before your declaration of "var"?
Was This Post Helpful? 1
  • +
  • -

#5 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 5
  • View blog
  • Posts: 396
  • Joined: 05-February 10

Re: Classes issue

Posted 24 July 2011 - 08:09 PM

I do, except with more errors:
-`Calculator' does not name a type
- In function `int main()':
- `var' undeclared (first use this function)
- (Each undeclared identifier is reported only once for each function it appears in.)
- [Build Error] [Main.o] Error 1

This post has been edited by Java Student: 24 July 2011 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Classes issue

Posted 24 July 2011 - 09:00 PM

Your class definition is missing the terminating semi-colon:

class Calculator{
      public:
      void myFunc();
}



should be:

class Calculator {
public:
	void myFunc();
};



Drop the class keyword from class Calculator var;.

Add #include <iostream> to the main function.

This post has been edited by n8wxs: 24 July 2011 - 09:07 PM

Was This Post Helpful? 2
  • +
  • -

#7 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 5
  • View blog
  • Posts: 396
  • Joined: 05-February 10

Re: Classes issue

Posted 24 July 2011 - 10:26 PM

SOLVED

Thanks, it was adding the #include <iostream> to the Calculator.cpp that did it.

:smile2:
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: Classes issue

Posted 25 July 2011 - 05:00 AM

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

// class Calculator var;
// you meant to do this
// Calculator var;
// however, globals are bad, so don't do that

int main() {
	// this is acceptable, and some people like it
	// but it's more standard to include after the includes
	// and will cause you less pain when you have more fuctions in the file
	using namespace std;

	// declare inside a function
	Calculator var;
	
	var.myFunc();    
	cout << endl;
	
	// don't do this, consider alternatives
	// system("pause");
	
	// this function returns a number, right?
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#9 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 5
  • View blog
  • Posts: 396
  • Joined: 05-February 10

Re: Classes issue

Posted 25 July 2011 - 11:52 AM

Thanks for the comments!

Yeah im not sure how my program runs even when my main() didn't return a value :turned:
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: Classes issue

Posted 25 July 2011 - 12:28 PM

View PostJava Student, on 25 July 2011 - 02:52 PM, said:

Yeah im not sure how my program runs even when my main() didn't return a value :turned:


It's a quirk of C/C++. Even when a return type is defined, the language doesn't get too worked up if you don't return anything. You usually won't get a compile error.
Was This Post Helpful? 0
  • +
  • -

#11 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 5
  • View blog
  • Posts: 396
  • Joined: 05-February 10

Re: Classes issue

Posted 25 July 2011 - 01:13 PM

Ah i see.

I thought i had this solved, but now for some reason i have more errors and now im thinking its my compiler.

Main.cpp

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

// class Calculator var;
// you meant to do this
// Calculator var;
// however, globals are bad, so don't do that

int main() {
	// this is acceptable, and some people like it
	// but it's more standard to include after the includes
	// and will cause you less pain when you have more fuctions in the file
	using namespace std;

	// declare inside a function
	Calculator var;
	
	var.myFunc();    
	cout << endl;
	
	// don't do this, consider alternatives
	// system("pause");
	
	// this function returns a number, right?
	return 0;
}



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

void Calculator::myFunc() {
     cout << "blah";
}



Calculator.h
#ifndef Calculator_H
#define Calculator_H

class Calculator{
      public:
      void myFunc();
}

#endif



Errors:
- new types may not be defined in a return type
- extraneous `int' ignored
- `main' must return `int'
- [Build Error] [Main.o] Error 1

Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 871
  • View blog
  • Posts: 3,996
  • Joined: 09-June 09

Re: Classes issue

Posted 25 July 2011 - 01:22 PM

your still missing your semi colon at the end of your class declaration

#ifndef Calculator_H
#define Calculator_H

class Calculator{
      public:
      void myFunc();
}; //<--- HERE

#endif


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1