Difficulty Accessing Functions from A Class File

Everything I've looked up on accessing functions from a class only shows how to access a class inside the same .cpp file. The only thing I was able to find regarding a separate file is how to call an constructor. To quickly alleviate this problem, I decided to try calling the functions that I needed to access in the constructor. This resulted in nothing from the various functions actually being implemented and a blank prompt shown when the program is run. Why does this happen and how could I cause this to work? Alternatively, is there a way to just call a function from main that exists in a class from another file?
Last edited on
Post your code.
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cstdio>
#include <string>
#include "Prog1.h"
using namespace std;

    void progChoice(); //prompt to choose which reference program to run
    void progSwitch(int &progC); //function that executes the chosen program
    bool progRes(bool &quit); //determines if the user wants to continue using this program or to exit

int main(){
    bool quit = false;
    while(quit == false){ //program loop, brings up the beginning prompt to run another program if the first ran successfully
    progChoice();
    progRes(quit);} //prompt to choose to continue with the program or exit the program
return 0;}

void progChoice(){
        int progC; //variable reprisents the choice of the user
                printf("Enter the number reprisenting the program you wish to run from the following list: ");
            cin>>progC;
    progSwitch(progC);} //function is called in this function because passed variable is declared here and therefore would not work in the main function

void progSwitch(int &progC){
        switch(progC){
case 1:
        Prog1 prog1(); //first program is run in this case, a simple calculator
break;
        case 2:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        case 3:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        case 4:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        case 5:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        default:
                printf("\nInvalid input, please try again\n");
    progChoice();} //invalid input calls the function to decide the choice again
}

bool progRes(bool &quit){
        char progR; //char reprisents choice to continue running program or exit
                printf("Would you like to run another program? Y/N\n\n");
            cin>> progR;
        if(progR == 'Y' || progR == 'y'){
        quit = false;}
        else if(progR == 'N' || progR == 'n'){
        quit = true;}else{
                printf("Your input was invalid\n\n");
            cin>> progR;}
return quit;}


class header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PROG1_H
#define PROG1_H
#include <string>
using namespace std;

class Prog1
{
    public:
        Prog1(int a, int b, string type);
        void input(int a, int b, string type);
        int eqType();
    private:
        int _a;
        int _b;
        string _type;
};
#endif // PROG1_H 


class cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Prog1.h"
#include <cstdio>
#include <iostream>

Prog1::Prog1(int a, int b, string type){
        a = _a;
        b = _b;
        type = _type;
        input(a, b, type);}

void Prog1::input(int a, int b, string type){
                printf("\nEnter the first number of the equation here:");
            cin>> a;

                printf("\nEnter the second number of the equation here:");
            cin>> b;} //remainder of the program has yet to be written but is in a state where it will still run without errors if other errors are resolved

            int Prog1::eqType(){return 0;} //see previous comment 


I know it may not be organized the best, which is why I was reluctant to show it, but here it is.
Didn't look through all of it, might edit more later.

First thing I see:
1
2
3
4
5
6
Prog1::Prog1(int a, int b, string type){
        a = _a;
        b = _b;
        type = _type;
        input(a, b, type);
}

You're doing it backwards. You are assigning the value of _a to a, where "a" is the parameter of your constructor. _a is uninitialized at this point. Same for the others.

Do this:
1
2
3
4
5
Prog1::Prog1(int a, int b, string type){
        this->_a = a;
        this->_b = b;
        this->_type = type;
}

Note that the "this->" prefix is not needed in this case, if the member name is different than the parameter name.

Your input() member function doesn't make a lot of sentence either, due to a similar confusion. "a" is just the parameter of the function, and has nothing to with your _a member function. When you do cin, this isn't being saved.

Instead of your input() function, I would rather have a default constructor that then asks you to fill in _a and _b.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Prog1::Prog1()
{
    int a_temp;
    int b_temp;
    std::string type_temp;

    cin >> a_temp;
    _a = a_temp;

    cin >> b_temp;
    _b = b_temp;

    cin >> type_temp;
    _type = type_temp;
}


----------------------------------

Other edit:
If the code is set up in the way described above,
1
2
3
case 1:
        Prog1 prog1(); //first program is run in this case, a simple calculator
break;

should not actually execute anything other than the default constructor (which you didn't previously have!!) itself.

You would want to something like:
1
2
3
4
case 1:
        Prog1 prog1(); //first program is run in this case, a simple calculator
        prog1.eqType(); //not sure if this is what you want, but it would be like "running" the program once you created it
break;
Last edited on
Other edit:
If the code is set up in the way described above,
1
2
3
case 1:
        Prog1 prog1(); //first program is run in this case, a simple calculator
break;


should not actually execute anything other than the default constructor (which you didn't previously have!!) itself.

You would want to something like:
1
2
3
4
case 1:
        Prog1 prog1(); //first program is run in this case, a simple calculator
        prog1.eqType(); //not sure if this is what you want, but it would be like "running" the program once you created it
break;


Exactly, I wanted it to run the constructor, which was then supposed to call the other functions in the class. I did this because what I wanted to do at first was just create the object and then call the two functions in it like so:

1
2
3
Prog1 prog1();
prog1.input();
prog1.eqType();

but when I tried that, I got errors which I had no idea how to resolve.

36|error: request for member 'input' in 'prog1', which is of non-class type 'Prog1()'|
37|error: request for member 'eqType' in 'prog1', which is of non-class type 'Prog1()'
Have you implemented a default (AKA no-parameter) constructor in your code at all?
Because in your second post, you don't have one. You only have Prog1(int a, int b, string type); You should define Prog1(); along with Prog1::Prog1() {}
Last edited on
I forgot about the parameters. I assumed something else was wrong because the errors say nothing about that. Would I just need the parameters of the object to match that or do I actually need a default constructor for this to work? Sorry if that sounds like a stupid question, I'm still very much in the beginning stages of learning c++.
In standard C++, it is best to give an explicit default constructor definition (as simple as adding the line
Prog1() {}; in your header file for now). I believe some things can fail if you do not have one (someone more experienced than me might be able to give specifics). For your code, I would try putting in an empty default constructor and see if it changes any error messages you're getting, and we'll see if that fixes it or not.
Last edited on
Okay, I added the default constructor, but still got the same errors. I also commented out the other constructor and moved the variables from the first constructor into the input function to see if that helped. I still have the same errors.
One last thing... if this doesn't work, sorry. Get rid of the parenthesis when you make your object in the switch case part.
1
2
3
case 1:
        Prog1 prog1; //first program is run in this case, a simple calculator
     ...
Last edited on
Well, that got rid of that error, but a new one appeared:

35|error: no matching function for call to 'Prog1::Prog1()'|
Can you paste your header file again to see what's changed?
Edit: If you defined the default constructor like Prog1() {}; in the header, it doesn't need a body in the relative .cpp file, so get rid of that.
Last edited on
Okay, here's what they look like now:

main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cstdio>
#include <string>
#include "Prog1.h"
using namespace std;

    void progChoice(); //prompt to choose which reference program to run
    void progSwitch(int &progC); //function that executes the chosen program
    bool progRes(bool &quit); //determines if the user wants to continue using this program or to exit

int main(){
    bool quit = false;
    while(quit == false){ //program loop, brings up the beginning prompt to run another program if the first ran successfully
    progChoice();
    progRes(quit);} //prompt to choose to continue with the program or exit the program
return 0;}

void progChoice(){
        int progC; //variable reprisents the choice of the user
                printf("Enter the number reprisenting the program you wish to run from the following list: ");
            cin>>progC;
    progSwitch(progC);} //function is called in this function because passed variable is declared here and therefore would not work in the main function

void progSwitch(int &progC){
        switch(progC){
case 1:
        Prog1 prog1; //first program is run in this case, a simple calculator
        prog1.input();
        prog1.eqType();
break;
        case 2:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        case 3:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        case 4:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        case 5:
                printf("\nPlaceholder code: this program does not yet exist\n");
break;
        default:
                printf("\nInvalid input, please try again\n");
    progChoice();} //invalid input calls the function to decide the choice again
}

bool progRes(bool &quit){
        char progR; //char reprisents choice to continue running program or exit
                printf("Would you like to run another program? Y/N\n\n");
            cin>> progR;
        if(progR == 'Y' || progR == 'y'){
        quit = false;}
        else if(progR == 'N' || progR == 'n'){
        quit = true;}else{
                printf("Your input was invalid\n\n");
            cin>> progR;}
return quit;}


header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef PROG1_H
#define PROG1_H
#include <string>
using namespace std;

class Prog1
{
    public:
        Prog1(){};
        void input();
        int eqType();
    private:
        int _a;
        int _b;
        string _type;
};
#endif // PROG1_H  


other cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Prog1.h"
#include <cstdio>
#include <iostream>

/*Prog1::Prog1(int a, int b, string type){
        _a = a;
        _b = b;
        _type = type;
        input(a, b, type);}
*/

void Prog1::input(){

        int a, b;
                printf("\nEnter the first number of the equation here:");
            cin>> a;

                printf("\nEnter the second number of the equation here:");
            cin>> b;} //remainder of the program has yet to be written but is in a state where it will still run without errors if other errors are resolved

            int Prog1::eqType(){return 0;} //see previous comment  


For some reason, this caused a different series of errors:

main.cpp||In function 'void progSwitch(int&)':|
39|error: jump to case label [-fpermissive]|
35|error: crosses initialization of 'Prog1 prog1'|
42|error: jump to case label [-fpermissive]|
35|error: crosses initialization of 'Prog1 prog1'|
45|error: jump to case label [-fpermissive]|
35|error: crosses initialization of 'Prog1 prog1'|
48|error: jump to case label [-fpermissive]|
35|error: crosses initialization of 'Prog1 prog1'|
51|error: jump to case label [-fpermissive]|
35|error: crosses initialization of 'Prog1 prog1'|

(They also weren't in order. I don't know if that matters, but they're usually in order.)
Topic archived. No new replies allowed.