Compilierprobleme mit String-Vector
-
ich habe gestern die Anzahl der SB strings um 4 verringert mit dem Ergebnis, dass bei einer capacity=250 und einer size=16 mein Kaffee beim Warten nicht mehr kalt wird.
Jedoch benötige ich eine capacity=1750 !
Da ich C++ noch nicht so beherrsche, teile mir doch mit, ob ich diese richtig verstanden habe :
-
wie und wo implemetiere ich einen zusätzlichen SB Vector ?
etwa so : std::vectorstd::string my_sb(10," ") -
Sollten alle zeilen im Header- und Source File des Copy-Konstruktors
gelöscht werden ? -
Sollten alle zeilen im Header- und Source File des op=
gelöscht werden ? -
Wie soll ich die Strings als const & übergeben ?
Vielen Dank für Deine Antwort
ElectricProg
-
-
electricProg schrieb:
- wie und wo implemetiere ich einen zusätzlichen SB Vector ?
etwa so : std::vectorstd::string my_sb(10," ")
Ja, das sieht nicht schlecht aus:
in der Deklaration der Klasse:std::vectorstd::string sb;
im Konstruktor: auf richtige Größe bringen und mit " " initialisieren, in der Initialisierungsliste
MANUAL():sb(10," ")
{
// restlicher Code
}electricProg schrieb:
-
Sollten alle zeilen im Header- und Source File des Copy-Konstruktors
gelöscht werden ? -
Sollten alle zeilen im Header- und Source File des op=
gelöscht werden ?
Ja, genau. Der Compiler erzeugt automatisch solche Operationen. Und in diesem Fall funktionieren die auch wie gewünscht. Selber implementieren mußt Du nur, wenn Du zum Beispiel rohe Zeiger verwendest, da sonst nur der Zeiger kopiert wird und das neue Objekt sich das Objekt auf das der Zeiger zeigt mit dem alten teilt.
electricProg schrieb:
- Wie soll ich die Strings als const & übergeben ?
Du hast im Konstruktor mehrmals const string übergeben.
Beispiel:
// Hier wird der string bei der Übergabe kopiert. // Es kostet also jede Übergabe eine Kopie und gerade bei großen Strings macht // das recht viel aus void foo(const string name) { ... } // besser: // Referenz wird übergeben, keine Kopie void foo(const string & name) { ... }
MfG Jester
- wie und wo implemetiere ich einen zusätzlichen SB Vector ?
-
Hallo Jester,
ich habe versucht, Deine Postings und Vorschläge in meine Dateien einzubauen.
Jedoch gibt es eine Menge Fehler beim Compilieren.Ich poste zum Verständnis die Source- und Header Dateien:
/////////////////////////////////////////////////////////// // MANUAL.h // Implementation of the Class MANUAL // Created on: 08-Jan-2004 11:02:12 /////////////////////////////////////////////////////////// #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; std::vector<std::string> sb; class MANUAL { public: MANUAL(); MANUAL( const short my_num_ch, const std::string & my_nam_ch, const short my_num_se, const std::string & my_nam_se, const short my_num_ss, const std::string & my_nam_ss, const short my_page, const short my_sheet, const std::string & my_pict, const std::vector<std::string> & my_sb, const std::string & my_pict_rev, const std::string & my_rev_date ); ~MANUAL(); short get_num_ch() const ; std::string get_nam_ch() const ; short get_num_se() const ; std::string get_nam_se() const ; short get_num_ss() const ; std::string get_nam_ss() const ; short get_page() const ; short get_sheet() const ; std::string get_pict() const ; std::string get_sb(int index) const ; std::string get_pict_rev() const ; std::string get_rev_date() const ; short num_ch; std::string nam_ch; short num_se; std::string nam_se; short num_ss; std::string nam_ss; short page; short sheet; std::string pict; std::string sb[10]; std::string pict_rev; std::string rev_date; };
/////////////////////////////////////////////////////////// // MANUAL.cpp // Implementation of the Class MANUAL // Created on: 08-Jan-2004 11:02:13 /////////////////////////////////////////////////////////// #include "MANUAL.h" // Initialisierung der Elementvariablen // Standard Konstruktor MANUAL::MANUAL() : num_ch(0), nam_ch(" "), num_se(0), nam_se(" "), num_ss(0), nam_ss(" "), page(0), sheet(0), pict(" "), sb(5," "), pict_rev(" "), rev_date(" ") { } // Kopier Konstruktor MANUAL::MANUAL( const short my_num_ch, const std::string & my_nam_ch, const short my_num_se, const std::string & my_nam_se, const short my_num_ss, const std::string & my_nam_ss, const short my_page, const short my_sheet, const std::string & my_pict, const std::string & my_sb, const std::string my_pict_rev, const std::string my_rev_date ) : num_ch(my_num_ch),nam_ch(my_nam_ch),num_se(my_num_se),nam_se(my_nam_se), num_ss(my_num_ss),nam_ss(my_nam_ss),page(my_page),sheet(my_sheet),pict(my_pict), sb(my_sb),pict_rev(my_pict_rev),rev_date(my_rev_date) { } MANUAL::~MANUAL(){ } short MANUAL::get_num_ch() const { return num_ch; } std::string MANUAL::get_nam_ch() const { return nam_ch; } short MANUAL::get_num_se() const { return num_se; } std::string MANUAL::get_nam_se() const { return nam_se; } short MANUAL::get_num_ss() const { return num_ss; } std::string MANUAL::get_nam_ss() const { return nam_ss; } short MANUAL::get_page() const { return page; } short MANUAL::get_sheet() const { return sheet; } std::string MANUAL::get_pict() const { return pict; } //std::string get_sb(int index) const ; //{ //return sb() //} std::string MANUAL::get_pict_rev() const { return pict_rev; } std::string MANUAL::get_rev_date() const { return rev_date; } ostream& operator<<(ostream& os, const MANUAL& rhs) { os << rhs.get_num_ch() << " " << rhs.get_nam_ch() << " " << rhs.get_num_se() << " " << rhs.get_nam_se(); return os; } template<class T> void ShowVector(const vector<T>& v) { //cout << "max_size() = " << v.max_size(); //cout << "\tsize() = " << v.size(); //cout << "\tcapacity() = " << v.capacity(); //cout << "\t" << (v.empty()? "leer": "nicht leer"); //cout << "\n"; for (int i = 0; i < v.size(); ++i) cout << v[i] << "\n"; cout << endl; } int main() { MANUAL A1(21,"AIR CONDITIONING",21,"AIR DISTRIBUTION",1,"RECIRC FAN LEFT",3,0,"a320_21_21_1_3_0.tif",("","","","",""),"","01.01.2000"); MANUAL A2(21,"AIR CONDITIONING",21,"AIR DISTRIBUTION",2,"RECIRC FAN RIGHT",3,0,"a320_21_21_2_3_0.tif",("","","","",""),"","01.01.2000"); MANUAL A3(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",1,"PWR SPLY",1,0,"a320_21_26_1_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A4(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",2,"BLOWER FAN",1,0,"a320_21_26_2_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A5(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",3,"SKIN AIR INLET VALVE",1,0,"a320_21_26_3_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A6(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",4,"SKIN EXCHANGER OUTLET VALVE",1,0,"a320_21_26_4_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A7(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",5,"COND AIR INLET VALVE",1,0,"a320_21_26_5_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A8(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",6,"FLOW & TEMP WARN/TEST",1,0,"a320_21_26_6_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A9(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",7,"OVRD EXTC & DITCHING",1,0,"a320_21_26_7_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A10(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",8,"PWR SPLY & AUTO CTL",1,0,"a320_21_26_8_1_0.tif",("","","","",""),"","01.01.2000"); vector<MANUAL> AWM201(10); vector<MANUAL>::iterator manual_iter; AWM201[0]= A1; AWM201[1]= A2; AWM201[2]= A3; AWM201[3]= A4; AWM201[4]= A5; AWM201[5]= A6; AWM201[6]= A7; AWM201[7]= A8; AWM201[8]= A9; AWM201[9]= A10; ShowVector(AWM201); //typedef vector<MANUAL>::iterator it1; //it1 p = find_if(AWM.begin(),AWM.end(), MANUAL_eq(21)); //if (p != AWM.end()) //{ //cout << "Cha num = " << (*p).get_num_ch() << " Cha nam = " << (*p).get_nam_ch() << endl; //cout << "Sec num = " << (*p).get_num_se() << " Sec nam = " << (*p).get_nam_se() << endl; //cout << "Sub num = " << (*p).get_num_ss() << " Sub nam = " << (*p).get_nam_ss() << endl; //} //else //{ //cout << " nothing found" << endl; //} system("pause"); return 0; }
<hume sagt>Aktivier doch bitte BBCode, wenn du Code postest!<hume sage>
-
/////////////////////////////////////////////////////////// // MANUAL.h // Implementation of the Class MANUAL // Created on: 08-Jan-2004 11:02:12 /////////////////////////////////////////////////////////// #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; class MANUAL { public: MANUAL(); ~MANUAL(); short get_num_ch() const ; const std::string & get_nam_ch() const ; short get_num_se() const ; const std::string & get_nam_se() const ; short get_num_ss() const ; const std::string & get_nam_ss() const ; short get_page() const ; short get_sheet() const ; const std::string & get_pict() const ; const std::string & get_sb(int index) const ; const std::string & get_pict_rev() const ; const std::string & get_rev_date() const ; short num_ch; std::string nam_ch; short num_se; std::string nam_se; short num_ss; std::string nam_ss; short page; short sheet; std::string pict; std::vector<std::string> sb; std::string pict_rev; std::string rev_date; };
/////////////////////////////////////////////////////////// // MANUAL.cpp // Implementation of the Class MANUAL // Created on: 08-Jan-2004 11:02:13 /////////////////////////////////////////////////////////// #include "MANUAL.h" // Initialisierung der Elementvariablen // Standard Konstruktor MANUAL::MANUAL() : num_ch(0), nam_ch(" "), num_se(0), nam_se(" "), num_ss(0), nam_ss(" "), page(0), sheet(0), pict(" "), sb(5," "), pict_rev(" "), rev_date(" ") { } // Kopier Konstruktor MANUAL::~MANUAL(){ } short MANUAL::get_num_ch() const { return num_ch; } const std::string & MANUAL::get_nam_ch() const { return nam_ch; } short MANUAL::get_num_se() const { return num_se; } const std::string & MANUAL::get_nam_se() const { return nam_se; } short MANUAL::get_num_ss() const { return num_ss; } const std::string & MANUAL::get_nam_ss() const { return nam_ss; } short MANUAL::get_page() const { return page; } short MANUAL::get_sheet() const { return sheet; } const std::string & MANUAL::get_pict() const { return pict; } const std::string & get_sb(int index) const ; { return sb.at(index) } const std::string & MANUAL::get_pict_rev() const { return pict_rev; } const std::string & MANUAL::get_rev_date() const { return rev_date; }
dürfte schonmal etwas besser sein.
Rückgabewerte bei strings, wenn es sich nicht um lokale Variablen handelt ebenfalls lieber per const &(Performance).Ansonsten kannst Du Dir überlegen, ob Du die ganzen getter nicht lieber direkt in der Klasse implementierst. Das sind eh nur einzeiler, aber so blähen sie die Implementierung unnötig auf.
MfG Jester
-
Hallo Jester,
wenn ich Deine Postings übernehme und die get Funktionen in der Header datei definiere, erhalte ich in allen ausser dem ersten string (get_nam_ch) folgende Fehlermeldung:
"
In member function std::string & Manual::get_nam_se() const: could not convert this -> MANUAL:nam_se to std::string &
"Was ist noch zu bereinigen ?
Ich poste nochmals beide Dateien:
/////////////////////////////////////////////////////////// // MANUAL.h // Implementation of the Class MANUAL // Created on: 08-Jan-2004 11:02:12 /////////////////////////////////////////////////////////// #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; std::vector<std::string> sb; class MANUAL { public: MANUAL(); ~MANUAL(); short get_num_ch() const {return num_ch;} std::string & get_nam_ch() const {return nam_ch;} short get_num_se() const {return num_se;} std::string & get_nam_se() const {return nam_se;} short get_num_ss() const {return num_ss;} std::string & get_nam_ss() const {return nam_ss;} short get_page() const {return page; } short get_sheet() const {return sheet; } std::string & get_pict() const {return pict; } std::string & get_sb(int index) const {return sb.at(index); } std::string & get_pict_rev() const {return pict_rev; } std::string & get_rev_date() const {return rev_date; } short num_ch; std::string nam_ch; short num_se; std::string nam_se; short num_ss; std::string nam_ss; short page; short sheet; std::string pict; std::vector<std::string> sb; std::string pict_rev; std::string rev_date; }; /////////////////////////////////////////////////////////// // MANUAL.cpp // Implementation of the Class MANUAL // Created on: 08-Jan-2004 11:02:13 /////////////////////////////////////////////////////////// #include "MANUAL.h" // Initialisierung der Elementvariablen // Standard Konstruktor MANUAL::MANUAL() : num_ch(0), nam_ch(" "), num_se(0), nam_se(" "), num_ss(0), nam_ss(" "), page(0), sheet(0), pict(" "), sb(5," "), pict_rev(" "), rev_date(" ") { } MANUAL::~MANUAL(){ } ostream& operator<<(ostream& os, const MANUAL& rhs) { os << rhs.get_num_ch() << " " << rhs.get_nam_ch() << " " << rhs.get_num_se() << " " << rhs.get_nam_se(); return os; } template<class T> void ShowVector(const vector<T>& v) { //cout << "max_size() = " << v.max_size(); //cout << "\tsize() = " << v.size(); //cout << "\tcapacity() = " << v.capacity(); //cout << "\t" << (v.empty()? "leer": "nicht leer"); //cout << "\n"; for (int i = 0; i < v.size(); ++i) cout << v[i] << "\n"; cout << endl; } int main() { MANUAL A1(21,"AIR CONDITIONING",21,"AIR DISTRIBUTION",1,"RECIRC FAN LEFT",3,0,"a320_21_21_1_3_0.tif",("","","","",""),"","01.01.2000"); MANUAL A2(21,"AIR CONDITIONING",21,"AIR DISTRIBUTION",2,"RECIRC FAN RIGHT",3,0,"a320_21_21_2_3_0.tif",("","","","",""),"","01.01.2000"); MANUAL A3(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",1,"PWR SPLY",1,0,"a320_21_26_1_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A4(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",2,"BLOWER FAN",1,0,"a320_21_26_2_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A5(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",3,"SKIN AIR INLET VALVE",1,0,"a320_21_26_3_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A6(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",4,"SKIN EXCHANGER OUTLET VALVE",1,0,"a320_21_26_4_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A7(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",5,"COND AIR INLET VALVE",1,0,"a320_21_26_5_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A8(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",6,"FLOW & TEMP WARN/TEST",1,0,"a320_21_26_6_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A9(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",7,"OVRD EXTC & DITCHING",1,0,"a320_21_26_7_1_0.tif",("","","","",""),"","01.01.2000"); MANUAL A10(21,"AIR CONDITIONING",26,"AVNCS EQPT VENT",8,"PWR SPLY & AUTO CTL",1,0,"a320_21_26_8_1_0.tif",("","","","",""),"","01.01.2000"); vector<MANUAL> AWM201(10); vector<MANUAL>::iterator manual_iter; AWM201[0]= A1; AWM201[1]= A2; AWM201[2]= A3; AWM201[3]= A4; AWM201[4]= A5; AWM201[5]= A6; AWM201[6]= A7; AWM201[7]= A8; AWM201[8]= A9; AWM201[9]= A10; ShowVector(AWM201); //typedef vector<MANUAL>::iterator it1; //it1 p = find_if(AWM.begin(),AWM.end(), MANUAL_eq(21)); //if (p != AWM.end()) //{ //cout << "Cha num = " << (*p).get_num_ch() << " Cha nam = " << (*p).get_nam_ch() << endl; //cout << "Sec num = " << (*p).get_num_se() << " Sec nam = " << (*p).get_nam_se() << endl; //cout << "Sub num = " << (*p).get_num_ss() << " Sub nam = " << (*p).get_nam_ss() << endl; //} //else //{ //cout << " nothing found" << endl; //} system("pause"); return 0; }
Vielen Dank
ElectricProg
-
die Referenz muß const sein...
Aber das steht eigentlich schon in meinem vorherigen Posting.
-
Hallo Jester,
das mit der Referenz ist gelöst, jedoch habe ich noch ein weiteres Problem:
- beim ersten Aufruf der Zeile
MANUAL A1(21,"AIR CONDITIONING",21,"AIR DISTRIBUTION",1,"RECIRC FAN LEFT",3,0,"a320_21_21_1_3_0.tif",("","","","",""),"","01.01.2000");
erscheint folgende Compiler Fehlermeldung:
"no matching function for call to 'MANUAL::MANUAL(int.const
candidates are: MANUAL::MANUAL(const MANUAL&
MANUAL::MANUAL()"
Was ist noch zu ändern ?
ElectricProg
- beim ersten Aufruf der Zeile
-
@electricProg
Es reicht!
Ich habe jetzt dreimal deine Beiträge editiert. Zweimal wurdest du bereits freundlich darauf hingewiesen, BBCode zu aktivieren, wenn du viel Code postest. Das ist doch nun wahrlich nicht zu viel verlangt, erleichtert anderen es aber erheblich, deinem Problem zu folgen.Also entweder du hälst dich an die Spielregeln oder ich werde deine Beiträge kommentarlos schließen!
-
Hallo HumeSikkens,
nach Deiner Info habe ich meine Code Postings einmal mit den Code Tags und einmal mit den C/C++ Tags umschlossen. Es war aber die Option "BBCode in diesem Beitrag deaktivieren" aktiviert ! Dies hat wahrscheinlich dazu geführt,
dass die Tags im der Nachricht nicht enthalten waren.Werde dies ändern und mich dran halten!
electricProg
-
Der Konstruktor dieser Form ist nicht drin.
Den mußt Du schon noch definieren.MfG Jester