-3

So I decided to start learning c++ I wanted to find out how to use classes in it. I thought I had set it up correctly (From looking at tutorials) but it gives me the following error..

C:\Dev-Cpp\main.cpp `Math' does not name a type 

I am new to c++ and the Dev-C++ Compiler so I haven't figured out the errors yet. HEre is my code..

Main class:

#include <cstdlib>
#include <iostream>

using namespace std;

//variables
int integer;
Math math;

int main()
{
    math = new Math();


    integer = math.add(5,2);

    cout << integer << endl;


    system("PAUSE");
    return EXIT_SUCCESS;
}

Here is my Math.cpp class:

#include <cstdlib>
#include <iostream>
#include "Math.h"

using namespace std;

public class Math {

    public Math::Math() {

    }

    public int Math::add(int one, int two) {
           return (one+two);      
    }

}

And the Math header file:

public class Math {
       public:
              public Math();
              public int add(int one, int two) {one=0; two=0};      

};

Any help would be appreciated, I have been trying to work this out for awhile now.

Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91
  • 1
    Are you including Math.h (in your main class)? – Chris Dargis Jul 03 '12 at 04:52
  • 5
    syntax of C++ is not same as Java. There are whole bunch of syntax errors in this code. Read a good C++ book for basic syntax. You can find a good list of books here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Naveen Jul 03 '12 at 04:52
  • Yeah, `Math math = new Math();` is not correct syntax. http://www.cplusplus.com/reference/std/new/operator%20new%5B%5D/ Also: `integer = m.add(5,2);`, I am not seeing an identifier `m` – Chris Dargis Jul 03 '12 at 04:53
  • @VanDarg Sorry that was a typo. Also could you give me an example of the correct syntax? – Duncan Palmer Jul 03 '12 at 04:57
  • @DuncanPalmer: Oops sorry, wrong link. http://www.cplusplus.com/reference/std/new/operator%20new/ `Math *math = new Math();` – Chris Dargis Jul 03 '12 at 04:58

1 Answers1

2

You are using a lot of Java-ish syntaxes. What you should have is this (untested):

//main.cpp

#include <cstdlib>
#include <iostream>
#include "Math.h"
using namespace std;

int main()
{
    //No Math m = new Math();
    //Either Math *m = new Math(); and delete m (later on) or below:
    Math m; //avoid global variables when possible.
    int integer = m.add(5,2); //its m not math.
    cout << integer << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

For Math.h

class Math { //no `public`
public:
    Math();
    int add(int, int);
};

For Math.cpp

#include <cstdlib>
#include <iostream> //unnecessary includes..
#include "Math.h"

using namespace std;

Math::Math() {

}

int Math::add(int one, int two) {
    return (one+two);
}

And compile the two cpp files (in case of gcc)

$ g++ main.cpp Math.cpp
$ ./a.out

It seems you are using Dev C++ IDE, in that case the IDE compiles and executes the program for you.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104