Raga guardate un po' questo codice:
base.h
base.cppcodice:#ifndef _BASE_ #define _BASE_ #include <string> class base { private: char c[10]; char* c2; std::string stringa; public: base(); ~base(); void setC(char[]); void setC2(char*); void setStringa(std::string); char getC(); char* getC2(); std::string getStringa(); }; #endif // _BASE_
il problema è il metodo setC, infatti se nel main.cpp scrivo:codice:#include "base.h" #include <string> using namespace std; base::base() { strcpy(c, "c1"); c2 = "c2"; stringa = "stringa"; } base::~base(){}; void base::setC(char c1[10]) { strcpy(c,c1); } void base::setC2(char* c) { c2 = c; } void base::setStringa(string s) { stringa = s; } char base::getC() { char tmp[10]; strcpy(tmp,c); return tmp[10]; } char* base::getC2() { return c2; } string base::getStringa() { return stringa; }
come risultato ho:codice:#include "base.h" #include <iostream.h> #include <string> void main() { base a; a.setStringa("stringa"); a.setC("c1"); a.setC2("c2"); cout << "a.c:" << a.getC() << endl; cout << "a.c2:" << a.getC2() << endl; cout << "a.stringa:" << a.getStringa().c_str() << endl; }
a.ccarattere strano);
a.c2: c2
a.stringa: stringa
Che errore ho fatto secondo voi?

Rispondi quotando

