Someone care to clarify this for me.

I have another problem in another post. However that's not the issue here. On a second program I followed all the instructions thoroughly as we were even given visual aid. However upon compilation I received about 40+ declaration errors and some other errors. I changed some of the code making it so that the scope resolution was the intended constructor. Now I'm only receiving errors regarding the declarations. Assistance would be needed.

Here I'll provide a lesser form of the code because I just need someone to analyse where i may be mistaken.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

#include "numberfun.h"

int main()
{
    int val1,val2,val3,val4;
    cout<<"Please enter 4 integers:  ";
    cin>>val1>>val2>>val3>>val4;

    NumberFun Trial(val1,val2,val3,val4);
   cout<<"Num1 has the value:  "<<Trial.getNum1() <<endl;
    cout<<"Num2 has the value:  "<<Trial.getNum2() <<endl;
   cout<<"Num3 has the value:  "<<Trial.getNum3() <<endl;
    cout<<"Num4 has the value:  "<<Trial.getNum4() <<endl;

return 0;
}

numberfun.cpp

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
42
43
#include "numberfun.h"

using namespace std;

NumberFun::NumberFun(int val1,int val2, int val3, int val4)
{
    setValues(val1,val2,val3,val4);
}


void setValues(int m, int r, int i, int k)

{
    setNum1(m);
    setNum2(r);
    setNum3(i);
    setNum4(k);
}
void setNum1(int n)// the value passed to setNum1 is stored inside the parameter n, n is then assigned to num1
{

    num1=n;
}

void setNum2(int n)
{

    num2=n;
}

void setNum3(int n)
{

    num3=n;
}

void setNum4(int n)
{

    num4=n;
}



numberfun.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef NUMBERFUN_H
#define NUMBERFUN_H

class NumberFun
{
public:
    NumberFun(int val1,int val2, int val3, int val4);

    void setValues(int m, int r, int i, int k);
    void setNum1(int n);
    void setNum2(int n);
    void setNum3(int n);
    void setNum4(int n);
   

private:

    int num1,num2,num3,num4;



};

#endif  



C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:14: error: 'setNum1' was not declared in this scope
C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:15: error: 'setNum2' was not declared in this scope
C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:16: error: 'setNum3' was not declared in this scope
C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:17: error: 'setNum4' was not declared in this scope
C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:22: error: 'num1' was not declared in this scope
C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:28: error: 'num2' was not declared in this scope
C:\Users\Closetgeekz\Desktop\NumberFun-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\..\NumberFun\numberfun.cpp:34: error: 'num3' was not declared in this scope

Last edited on
You declared methods but defined functions.
1
2
3
4
void NumberFun::setNum4(int n)
{
    num4=n;
}
In your numberfun.cpp

1
2
3
void NumberFun::setNum1(int n) { 
num1=n;
}


Much like the constructor, you must specify the scope in which the function is located, in this case it is located in the NumberFun class, so prefix each of your function definitions as such...

1
2
3
returntype classname::functionName(params...) {
// definition
}


Good luck.
Last edited on
Again I'm not mostly responsible for this, simply following instructions to the letter, but the little programmer in me couldn't help but question the errors.
Topic archived. No new replies allowed.