My class doesnt know strings

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
#ifndef ENCRYPTER_H
#define ENCRYPTER_H
#include <string>
#include <iostream>

class Encrypter
{
    public:
        Encrypter();
        string encodefunction (string mystring) {
            //getting length of input string
            int stringlength = mystring.length();
            //creating a for loop to manipulate
            //the chars in the string
            for (int i=0; i<stringlength; i++) {
                //adding 20 to the character ascii
                char stringchar = mystring[i] + 20;
                //creating the encoded string
                string encodedstring = encodedstring + stringchar;
                //returning the encoded string
                return encodedstring;
            }
        }
        string decodefunction (string mystring) {
            //getting length of input string
            int stringlength = mystring.length();
            //creating a for loop to manipulate
            //the chars in the string
            for (int i=0; i<stringlength; i++) {
                //adding 20 to the character ascii
                char stringchar = mystring[i] - 20;
                //creating the encoded string
                string encodedstring = encodedstring + stringchar;
                //returning the encoded string
                return encodedstring;
            }
        }
        virtual ~Encrypter();
    protected:
    private:
};

#endif // ENCRYPTER_H 


|10|error: 'string' does not name a type|
Being dumb, why doesn't my class know strings
string is part of the std namespace, so you need to say std::string

or you can do this:

1
2
3
4
5
class Encrypter
{
    using std::string;
    // ... the rest of your class here
};
|10|error: using-declaration for non-member at class scope|
figured it out
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
#ifndef ENCRYPTER_H
#define ENCRYPTER_H
#include <string>
#include <iostream>
using std::string;
class Encrypter
{
    public:
        Encrypter();
        string encodefunction (string mystring) {
            //getting length of input string
            int stringlength = mystring.length();
            //creating a for loop to manipulate
            //the chars in the string
            for (int i=0; i<stringlength; i++) {
                //adding 20 to the character ascii
                char stringchar = mystring[i] + 20;
                //creating the encoded string
                string encodedstring = encodedstring + stringchar;
                //returning the encoded string
                return encodedstring;
            }
        }
        string decodefunction (string mystring) {
            //getting length of input string
            int stringlength = mystring.length();
            //creating a for loop to manipulate
            //the chars in the string
            for (int i=0; i<stringlength; i++) {
                //adding 20 to the character ascii
                char stringchar = mystring[i] - 20;
                //creating the encoded string
                string encodedstring = encodedstring + stringchar;
                //returning the encoded string
                return encodedstring;
            }
        }
        virtual ~Encrypter();
    protected:
    private:
};

#endif // ENCRYPTER_H
 


have to declare the namespace for the class outside of it
osht I thought that worked

*tries*

huh! I guess you can't do that afterall. That seems stupid.

try this instead:

1
2
3
4
5
class Encrypter
{
    typedef std::string string;
    // ...
};



EDIT: I was intentionally avoiding telling you to put using globally as that pollutes the namespace and defeats the entire point of having namespaces in the first place.

But whatever. If that's how you want to do it, go for it.
Last edited on
Ok, trying the typedef; not that I wanted to do it a certain way. I just troubleshoot til it works lol

Thanx for help
Topic archived. No new replies allowed.