[C++]Konstruktor
Ostatnio zmodyfikowano 2015-11-24 07:30
pepe450 Temat założony przez niniejszego użytkownika |
[C++]Konstruktor » 2015-11-24 02:06:47 Witam mam problem z konstruktorem. Uczę się z książki "Szkoła programowania". Problem dotyczy tego konstruktora gdy próbuję go użyć. Person( const string & ln, const char * fn = "HejTy" ); Czy jest on poprawny, lub czy może ja błędnie go wywołuje? Roz10_2.o:Roz10_2.cpp|| undefined reference to `Person::Person(std::string const&, char const*)'| undefined reference to `Person::Person(std::string const&, char const*)'| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===| Jest to konstruktor z przykładowej klasy. #include<iostream> using namespace std; class Person { static const int LIMIT = 256; string lname; char fname[ LIMIT ]; public: Person() { lname = ""; fname[ 0 ] = '\0'; } Person( const string & ln, const char * fn = "HejTy" ); void Show() const; void FormalShow() const; }; int main() { Person one; Person two( "Staszek" ); Person three( "Jacek", "Placek" ); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); return 0; } void Person::Show() const { cout << "Imie: " << fname << endl; cout << "Nazwisko: " << lname << endl; } void Person::FormalShow() const { cout << "Nazwisko: " << lname << endl; cout << "Imie: " << fname << endl; }
|
|
michal11 |
» 2015-11-24 03:39:02 Nigdzie nie zdefiniowałeś tego konstruktora z 2 argumentami. Lepiej też będzie jak dodasz #include <string> . |
|
pepe450 Temat założony przez niniejszego użytkownika |
» 2015-11-24 06:21:45 Teraz zdefiniowałem i teraz mam następujący błąd. #include<iostream> #include<string> using namespace std; class Person { static const int LIMIT = 256; string lname; char fname[ LIMIT ]; public: Person() { lname = ""; fname[ 0 ] = '\0'; } Person( const string & ln, const char * fn = "HejTy" ) { lname = ln; fname = fn; } void Show() const; void FormalShow() const; }; int main() { Person one; Person two( "Staszek" ); Person three( "Jacek", "Placek" ); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); return 0; } void Person::Show() const { cout << "Imie: " << fname << endl; cout << "Nazwisko: " << lname << endl; } void Person::FormalShow() const { cout << "Nazwisko: " << lname << endl; cout << "Imie: " << fname << endl; }
C:\Users\pepe\Desktop\Szkoła program\Roz10_2.cpp||In constructor 'Person::Person(const string&, const char*)':| C:\Users\pepe\Desktop\Szkoła program\Roz10_2.cpp|18|error: incompatible types in assignment of 'const char*' to 'char [256]'| C:\Users\pepe\Desktop\Szkoła program\Roz10_2.cpp||In function 'int main()':| C:\Users\pepe\Desktop\Szkoła program\Roz10_2.cpp|26|error: no matching function for call to 'Person::Person(const char [8])'| C:\Users\pepe\Desktop\Szkoła program\Roz10_2.cpp|26|note: candidates are:|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
|
|
carlosmay |
» 2015-11-24 07:30:44 tak nie można przypisać tablicy char'ów. std::strcpy() |
|
« 1 » |