1

Error in assignment char :

structplusclass.cpp: In constructor ‘Soldado::Soldado(char, unsigned int, char, char, char)’: structplusclass.cpp:25:27: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] strcpy(mTodos.modelo, mol); ^ In file included from /usr/include/c++/5/cstring:42:0, from structplusclass.cpp:2: /usr/include/string.h:125:14: note: initializing argument 2 of ‘char* strcpy(char*, const char*)’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ structplusclass.cpp:27:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] strcpy(mTodos.material, mat); ^ In file included from /usr/include/c++/5/cstring:42:0, from structplusclass.cpp:2: /usr/include/string.h:125:14: note: initializing argument 2 of ‘char* strcpy(char*, const char*)’ extern char *strcpy (char *__restrict __dest, const char *__restrict __src) ^ structplusclass.cpp:28:18: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] strcpy(mNome, ns); 

Code:

#include <iostream> #include <cstring> #define SIZE 20 using namespace std; struct faca { char modelo[SIZE]; unsigned int peso; char material[15]; }; class Soldado { public: Soldado (char ns, unsigned int p, char mat, char arm, char mol); ~Soldado (); void Imprime(); protected: faca mTodos; char mNome[SIZE]; char mArmapri[SIZE]; }; Soldado::Soldado (char ns, unsigned int p, char mat, char arm, char mol) { strcpy(mTodos.modelo, mol); mTodos.peso = p; strcpy(mTodos.material, mat); strcpy(mNome, ns); strcpy(mArmapri, arm); } void Soldado::Imprime () { cout << "Soldado : " << mNome << ", " << "firearm : " << mArmapri << endl; cout << endl << " Faca : " << mTodos.modelo << ", " << "lenght : " << mTodos.peso << ", " << "Material : " << mTodos.material << endl; } Soldado::~Soldado () { mTodos.peso = 0; } int main() { char names1[SIZE], fire[SIZE]; char names2[SIZE], mat[15]; unsigned int eight; cout << "nome Soldado, nome arma de fogo " << endl; cin >> names1 >> fire; cout << "modelo faca e material " << endl; cin >> mat >> names2; cout << "peso" << endl; cin >> eight; Soldado brazil(names1, eight, mat, fire, names2); brazil.Imprime(); return 0; } 
2
  • 2
    Do you understand differences between const char*, char[] and char? It seems it is right time to read some good book for beginners. Commented May 6, 2018 at 3:17
  • Normally you should be scraping it all and moving to std::string as fast as you can. Is this some kind of school assignment where you are not allowed to use parts of the standard library? Commented May 6, 2018 at 8:10

1 Answer 1

1

Soldado::Soldado() takes the wrong parameters.

char names1[SIZE], fire[SIZE]; cout << "nome Soldado, nome arma de fogo " << endl; cin >> names1 >> fire; Soldado brazil(names1, ..., ..., fire, ...); 

Here names1 and fire are both declared as type char[SIZE]. What this means is that there are SIZE items of type char sitting in a row in memory (specifically, on the stack, but that isn't critical to know for this). But then you pass these on to your constructor, and your constructor is expecting this:

Soldado::Soldado (char ns, ..., ..., char arm, ...) { ... } 

Here, ns and arm are declared as type char, which is a single item of type char rather than a list of them like above.

The best easy fix to this problem would be to change ns and arm (as well as the other parameters that are expecting words) from type char to char[SIZE]. This will correct your compiler error, and get your code working.

However, there is a little bit more. Your constructor has a few lines like this:

strcpy(mTodos.modelo, mol); 

These will work perfectly fine as long as your user never types a name longer than 19 letters long (leaving one char to mark the end). But if they do type a longer name, your code will happily write past the end of the memory you have reserved for it. You should protect your memory with the `strncpy() function like this:

strncpy(mTodos.modelo, mol, SIZE); mTodos.modelo[SIZE - 1] = '\0'; 

Unfortunately, you need that second line to ensure that the string ends in a null character since strncpy() won't always do it for you.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.