Primer Aufgabe 10.2
-
Hallo,
ich bin dabei im Moment mit Hilfe von "Primer" C++ zu lernen und bin im Moment bei Aufgabe 10.2:
Leider bekomme ich eine Fehlermeldung, mit der ich nicht viel anfangen kann. Meine Lösung:
Header Datei:
#ifndef HEADER102_H_ #define HEADER102_H_ class Person { private: static const int LIMIT = 25; std::string lname; // Person’s last name char fname[LIMIT]; // Person’s first name public: //Person() {lname = ""; fname[0] = '\0'; } // #1 Person(); Person(const std::string & ln, const char * fn = "Heyyou"); // #2 ~Person(); // the following methods display lname and fname void Show() const; // firstname lastname format void FormalShow() const; // lastname, firstname format }; #endif
Funktionsdatei:
#include <iostream> #include <string> #include "header102.h" Person::Person() { lname="Nichts"; fname[0]='\0'; } Person::~Person() { } void Person::Show() const // firstname lastname format { using std::cout; using std::endl; cout<<"first name: "<<fname<<endl; cout<<"last name: "<<lname<<endl; } void Person::FormalShow() const // lastname, firstname format { using std::cout; using std::endl; cout<<"last name: "<<lname<<endl; cout<<"first name: "<<fname<<endl; }
Main Datei
#include <iostream> #include <string> #include <cctype> #include "header102.h" const int STKS=3; int main() { using std::cout; using std::ios_base; cout << "Using constructors to create new objects\n"; Person one; // use default constructor Person two("Smythecraft"); // use #2 with one default argument Person three("Dimwiddy", "Sam"); // use #2, no defaults cout.setf(ios_base::fixed, ios_base::floatfield);// #.## format cout.setf(ios_base::showpoint); one.Show(); one.FormalShow(); two.Show(); two.FormalShow(); three.Show(); three.FormalShow(); system("pause"); return 0; }
Leider bekomme ich beim Ausführen den folgenden Fehlercode:
Fehler 1 error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall Person::Person(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,char const *)" (??0Person@@QAE@ABV?char_traits@D@std@@V?$allocator@D@2@@std@@PBD@Z)" in Funktion "_main". C:\...\ConsoleApplication1\102.obj ConsoleApplication1
Kann mir bitte jemand Helfen??
Vielen Dank im Voraus!
-
// in deiner cpp-Datei fehlt die Implementation Person::Person(const std::string & ln, const char * fn) // ohne Defaultparameter { .. }
-
Danke sehr!
-
Ist das echt ein Beispiel vom Primer? Das ist ja mal scheiße. Ich dachte das Buch wäre besser.
Aus dem Kontext gezogen würde ich mal sagen dass dieses Array mit LIMIT durchaus schlechter Stil ist. Da würde ich sogar eher noch str.size() < LIMIT sagen als sonstwas.
Strings in Form einer char-Arrays haben in C++, die nichts mit C-Libs zusammentun, überhaupt nichts verloren.
-
wörtner schrieb:
Ist das echt ein Beispiel vom Primer? Das ist ja mal scheiße. Ich dachte das Buch wäre besser.
Aus dem Kontext gezogen würde ich mal sagen dass dieses Array mit LIMIT durchaus schlechter Stil ist. Da würde ich sogar eher noch str.size() < LIMIT sagen als sonstwas.
Strings in Form einer char-Arrays haben in C++, die nichts mit C-Libs zusammentun, überhaupt nichts verloren.
Ja, vor allem wenn man bedenkt, dass lname ein std::string ist. Ob da wohl mehr dahinter steckt...
-
Das ist nicht der "Primer" sondern der "Primer Plus".