I need help getting strings to work with my class. Integers, floats etc all seem to work, but strings won't. I think I need to use a pointer but I'm a noob and can't figure out how to do that.
Here's the code
Code:
#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;
class object
{
private:
char name[20];
char description[80]; // Longer description
int where; // Where object is, -1 if carried
public:
void setup(char n, char d, int w);
void desc(int room);
};
void object::setup(char n, char d, int w)
{
strcpy(name[20], n);
strcpy(description[100], d);
where = w;
}
void object::desc(int room)
{
if ((room == where) || (where == -1))
{
cout << "The cheese is "<< description << endl;
}
else
{
cout << "I can't find the " << name << ", can you?" << endl;
}
}
int main()
{
object cheese;
cheese.setup("Swiss cheese","holey and yellow",1);
cheese.desc(1);
cheese.desc(2);
}
and here are the errors:
Code:
object.cpp: In member function `void object::setup(char, char, int)':
object.cpp:20: error: invalid conversion from `char' to `char*'
object.cpp:20: error: invalid conversion from `char' to `const char*'
object.cpp:21: error: invalid conversion from `char' to `char*'
object.cpp:21: error: invalid conversion from `char' to `const char*'
object.cpp: In function `int main()':
object.cpp:40: error: invalid conversion from `const char*' to `char'
object.cpp:40: error: initializing argument 1 of `void object::setup(char,
char, int)'
object.cpp:40: error: invalid conversion from `const char*' to `char'
object.cpp:40: error: initializing argument 2 of `void object::setup(char,
char, int)'