My class doesnt know strings
Aug 15, 2010 at 2:53am UTC
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
Aug 15, 2010 at 2:55am UTC
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
};
Aug 15, 2010 at 3:01am UTC
|10|error: using-declaration for non-member at class scope|
Aug 15, 2010 at 3:16am UTC
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
Aug 15, 2010 at 3:17am UTC
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 Aug 15, 2010 at 3:18am UTC
Aug 15, 2010 at 4:04am UTC
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.