Source code seems not OK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdio.h>

using namespace std;

class Function
{
      public:
       int Addition(int a, int b)
       {
           return(a+b);
       }
       
       void Print()
       {
            cout << "Hello! This works.\n";
       }
}

int main()
{
    int a, b, c;
    cout << "Welcome! Fill in a number: ";     
    cin >> a;
    cout << "Fill in another number: ";
    cin >> b;
    
    c = Function.Addition(a, b);
    
    printf("The answer is %c.\n", c);
    
    Function.Print();
    
    cout << "Bye bye! Press ENTER to quit.";
    cin.get();
    return(0);
}    
    
            
       
           


This is my source code, but when i compile it with Dev-C++, it gives this errors:

21 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Function_In_Struct.cpp new types may not be defined in a return type

21 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Function_In_Struct.cpp extraneous `int' ignored

21 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Function_In_Struct.cpp `main' must return `int'

C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Function_In_Struct.cpp In function `int main(...)':

28 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Function_In_Struct.cpp expected primary-expression before '.' token

32 C:\Users\eigenaar\Documents\Eigen bestanden\C++\Source Code\Function_In_Struct.cpp expected primary-expression before '.' token

Can someone please help me?
Last edited on
You forgot a semicolon after the class declaration. Also, I hope you know that's not what classes are for, a namespace would be more appropriate. And why are you mixing cout and printf?
Thanks a lot! Now it works. I used printf() because I wanted to experiment with it. But what do you mean with "a namespace would be more appropriate"?

In any case thanks.
closed account (o3hC5Di1)
Hi there,

He means that classes are not used to group a bunch of functions.
Classes are blueprints for objects, just like in real life you have a plan for a house, and the actual house itself.

Namespaces are used to declare data (like variables or functions) in a specified scope, to avoid mixing names of those variables or functions.

The tutorial on this site has a good explanation (read: better than i could give) on both.

All the best,
NwN
Thanks NwM, I got it.
Topic archived. No new replies allowed.