OOP question

I originally started out trying to make a OOP that would add two numbers together using a method, and didn't use getter or setter functions. Well I failed. My understanding is that I am supposed to declare my method in the header file and then define it in the addition.cpp file.

addition.h
1
2
3
4
5
6
7
8
9
10
11
12
13
    class Addition
    {

    private:
	
    public:
	Addition();
	~Addition();
	int i;
	int j;
	
	void numbers();
};


addition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
      #include "addition.h"
      Addition::Addition()
      {
	
      }
      void Addition::numbers(int x, int y)
      {
	
      }

      Addition::~Addition()
     {
     }

 
  


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include"addition.cpp"
  
  
int main()
{
	Addition A1;

	



	system("Pause");
	return 0;
}

  

I've tried mixing and matching several different method declerations for numbers but nothing works. What am I doing wrong? Is this just an outright no-no. Should I use the constructor instead? I think the constructor is just a way for main to call each object.


Severity Code Description Project File Line Suppression State
Error (active) declaration is incompatible with "void Addition::numbers()" (declared at line 15 of...
What am I doing wrong?

1) Read compilers clarifications more carefully: it’s trying to help you.
In this case, it says what you write in your header file doesn’t match what you write in your source file, i.e. in the header file:
void numbers();
that's a function which doesn’t accept parameters and returns nothing
In the source file:
void Addition::numbers(int x, int y)
that's a function which accepts two parameters and returns nothing.

2) In general, the idea is to include header files, not source files.
In your main.cpp:
#include"addition.cpp"
should be:
#include "addition.h"

Note: in general, you should link against source file, not include them. If you don’t know about this topic, it means your developing environment is taking care of it for you.

3) The compiler will add the constructors you don't provide (again: in general). There're details to know (the so called rule of three/five/zero)
http://en.cppreference.com/w/cpp/language/rule_of_three
but, at this stage of your study, perhaps you just need to know you'd better not to declare the constructors you aren't going to define.

I think the constructor is just a way for main to call each object.

I’d said to create or initialise each object.

4) If you don’t want to read the compilers reports, at least try to be more accurate when you copy & paste on the forum:
- paste the entire error description;
- post the code in a way that the error line pointed out by the compiler (“declared at line 15 of”) matches the line in your code.

Here’s an example of working code:
Addition.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CATSONAMRS_ADDITION_H
#define CATSONAMRS_ADDITION_H

class Addition
{
public:
    int i;
    int j;
    
    void numbers(int x, int y);
};

#endif // CATSONAMRS_ADDITION_H 


Addition.cpp:
1
2
3
#include "Addition.h"

void Addition::numbers(int x, int y) {}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Addition.h"
#include <iostream>
#include <limits>

int main()
{
    Addition A1;
    // system("Pause"); <-- http://www.cplusplus.com/forum/beginner/1988/
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

Topic archived. No new replies allowed.