How do you put parameters properly in a constructor?

How do you put parameters in a constructor and make it work?
I'm getting an error in my Satellite1.h header saying
that the string does not name a type?

it's the

class Satellite1
{
public:
Satellite1(string a); <- this one is the problem :(

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
59
60
61
62
63
64
=====Main.cpp====

#include "Satellite1.h"
#include <iostream>


using namespace std;


int main()
{
    Satellite1 Nereid("Henry");
    Nereid.print();

    return 0;
}




=====Satellite1.h file====

#ifndef SATELLITE1_H
#define SATELLITE1_H



class Satellite1
{
    public:
        Satellite1(string a);
        void print();

    protected:

    private:
        string name;
};

#endif 


=====Satellite1.cpp file====

#include "Satellite1.h"

#include <iostream>


using namespace std;


Satellite1::Satellite1(string a)
{
    name=a;
}

void Satellite1::print(){
cout<<name<<endl;


}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
=====Satellite1.h file====

#ifndef SATELLITE1_H
#define SATELLITE1_H

#include <string>
using std::string;

class Satellite1
{
    public:
        Satellite1(string a);
        void print();

    protected:

    private:
        string name;
};

#endif  
thanks!! worked well :D
As a general rule, you shouldn't have a using statement inside a header file. Because you can't "un-using" it, the using will apply to all the other header files that you might include after it. This can cause name conflicts that are really hard to fix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
=====Satellite1.h file====

#ifndef SATELLITE1_H
#define SATELLITE1_H

#include <string>

class Satellite1
{
    public:
        Satellite1(std::string a);
        void print();

    protected:

    private:
        std::string name;
};

#endif   



Last edited on
I'll keep that in mind
Topic archived. No new replies allowed.