Not proper setting of a char array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include "iostream"
class tire {
public:
	char material[20]; // char error here
	double weight;
	char tiretype;};
class vehicle {
public:
	int tires;
	int doors;
	double virtual area() { return 0;}};
class car: public vehicle {
public:
	tire tireinfo;};
class semi: public vehicle {
	tire tireinfo;
	double area(double x, double y) { return x*y;}};

void main() {
car lexus;
lexus.tires = 2;
lexus.tireinfo.material = "rubber";
std::cout << lexus.tireinfo.material << std::endl; }


error C2440: '=' : cannot convert from 'const char [7]' to 'char [20]'



Really simple, but hey, I'm no C++ expert. :3
Either...

#include <cstring>

and turn this -> lexus.tireinfo.material = "rubber"; into this -> strcpy(lexus.tireinfo.material,"rubber");

Or...

#include <string>

and turn this -> char material[20]; into this -> std::string material;

I'd prefer the second solution.
Last edited on
Kewl. So "string" is part of the std library, basically?
Yeap.
Tanks! :3
Btw, it should be int main()
Topic archived. No new replies allowed.