Help with .cpp and .h CLASS REUSE

Feb 22, 2016 at 2:51am
Write your question here.

Hey guys so I'm trying to get my program to work I'm in the final steps. I separated it into 3 files, privatizing my class into my main.cpp, joshsClass.cpp, and joshsClass.h.

Cant get it to run, the only error I'm getting is in the main.cpp function, ill comment it in the code.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include "joshsClass.h"

using namespace std;



int main()
{
    joshsClass key;
    
    key.getName("Joshy Soshy Woshy"); // " Too many arguments to function called expected 0, have 1
    
    cout << key.getName() << endl<<endl;
    
    cout << "PROGRAM 'Variables in a Class' MADE BY JOSH GIALIS ON 2/14/16. \n\n\n ";
}




joshsClass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "joshsClass.h"

    
joshsClass::joshsClass() // Default Constructor
        {
            string name;
        }

joshsClass::joshsClass (string x) // Parameterized Constructor
        
        {
           name = x;
        }

        
string joshsClass::getName()
        
        {
            return name;
        }
        


joshsClass.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
25
#pragma once
#include <string>


using namespace std;

class joshsClass

{

    private:
    
        string name;
      

    public:
    
        joshsClass(); //Default Constructor
    
        joshsClass (string x); //Parameterized Constructor
    
        string getName();
    
    
};
Last edited on Feb 22, 2016 at 2:52am
Feb 22, 2016 at 3:01am
The error is pretty obvious. Look at your getName() function -

string joshsClass::getName()
It takes 0 arguments in paramter. So why are you feeding it one?

You have a constructor which takes a string and initializes it to "name". That's what you want to use -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include "joshsClass.h"

using namespace std;



int main()
{
    joshsClass key("Joshy Soshy Woshy");
    
    cout << key.getName() << endl<<endl;
    
    cout << "PROGRAM 'Variables in a Class' MADE BY JOSH GIALIS ON 2/14/16. \n\n\n ";
}
Feb 22, 2016 at 3:22am
Thanks Tarik, I tried analyzing it and caught that error I just wasn't sure how to fix it. I didn't know that in declaring the class in the main i could use

joshsClass key and then insert parenthesis.
Topic archived. No new replies allowed.