Ich bin (etwas) neu in C++ und ich habe versucht, ein Programm zum Laufen zu bringen. Alles kompiliert, bis ich anfange, einen Klassenkonstruktor in der Quelldatei zu definieren, obwohl ich es in der Header-Datei deklariert habe. Ich bin nicht sicher, ob es daran liegt, dass ich nicht alle meine Methoden definiert habe, weil dies mein zweiter Versuch ist, sie zu definieren (ich schreibe sie buchstäblich langsam um, um zu sehen, ob es hilft).(C++) Warum verursacht das Definieren eines Konstruktors, dass mein Programm nicht kompiliert?
Der betreffende Code unten ist (das ist, was auf der CPP-Datei ist):
#include <cstdlib>
#include <iostream>
#include <string.h>
#include "StockItem.h"
Diode::Diode(char* componentT, char* code, int quantity, int price){
strcpy(componentType,componentT);
strcpy(stockCode,code);
stockQuantity = quantity;
unitPrice = price;
}
Der Ausgang ist unter I erhalten:
cd 'G:\EDUCATION\University\Year 2\Year Courses\PROGRAMMING 2\Assignments\Assignment 2\COURSEWORK_100088688\StockProgram_C++'
C:\cygwin\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/g/EDUCATION/University/Year 2/Year Courses/PROGRAMMING 2/Assignments/Assignment 2/COURSEWORK_100088688/StockProgram_C++'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/stockprogram_c__.exe
make[2]: Entering directory '/cygdrive/g/EDUCATION/University/Year 2/Year Courses/PROGRAMMING 2/Assignments/Assignment 2/COURSEWORK_100088688/StockProgram_C++'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/StockItem.o.d"
g++ -c -g -std=c++11 -MMD -MP -MF "build/Debug/Cygwin-Windows/StockItem.o.d" -o build/Debug/Cygwin-Windows/StockItem.o StockItem.cpp
StockItem.cpp: In member function 'void StockItem::setComponentType(char*)':
StockItem.cpp:27:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char* S1 = "resistor";
^
StockItem.cpp:28:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char* S2 = "capacitor";
^
StockItem.cpp:29:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char* S3 = "transistor";
^
StockItem.cpp:30:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char* S4 = "diode";
^
StockItem.cpp:31:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
char* S5 = "ic";
^
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/StockProgram.o.d"
g++ -c -g -std=c++11 -MMD -MP -MF "build/Debug/Cygwin-Windows/StockProgram.o.d" -o build/Debug/Cygwin-Windows/StockProgram.o StockProgram.cpp
mkdir -p dist/Debug/Cygwin-Windows
g++ -o dist/Debug/Cygwin-Windows/stockprogram_c__ build/Debug/Cygwin-Windows/StockItem.o build/Debug/Cygwin-Windows/StockProgram.o
build/Debug/Cygwin-Windows/StockItem.o: In function `ZN9StockItemC2Ev':
/cygdrive/g/EDUCATION/University/Year 2/Year Courses/PROGRAMMING 2/Assignments/Assignment 2/COURSEWORK_100088688/StockProgram_C++/StockItem.h:8: undefined reference to `vtable for StockItem'
build/Debug/Cygwin-Windows/StockItem.o:StockItem.cpp:(.rdata$_ZTV5Diode[__ZTV5Diode]+0x8): undefined reference to `StockItem::getComponentInfo()'
build/Debug/Cygwin-Windows/StockItem.o:StockItem.cpp:(.rdata$_ZTV5Diode[__ZTV5Diode]+0xc): undefined reference to `StockItem::setComponentInfo(char*)'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:63: recipe for target 'dist/Debug/Cygwin-Windows/stockprogram_c__.exe' failed
make[2]: *** [dist/Debug/Cygwin-Windows/stockprogram_c__.exe] Error 1
make[2]: Leaving directory '/cygdrive/g/EDUCATION/University/Year 2/Year Courses/PROGRAMMING 2/Assignments/Assignment 2/COURSEWORK_100088688/StockProgram_C++'
nbproject/Makefile-Debug.mk:60: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/g/EDUCATION/University/Year 2/Year Courses/PROGRAMMING 2/Assignments/Assignment 2/COURSEWORK_100088688/StockProgram_C++'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 5s)
Und falls Menschen es brauchen, Dies ist die betreffende Klasse:
class Diode : public StockItem {
public:
Diode(char* componentT, char* code, int quantity, int price);
Diode(const Diode &D);
Diode& operator=(const Diode& D);
~Diode();
};
Meine Frage ist, warum bewirkt dies die Kompilierung? Jede Rückmeldung (unabhängig von der Relevanz) wird sehr geschätzt, danke.
Haben Sie http://stackoverflow.com/questions/3065154/undefined-reference-to-vtable angeschaut? – NPE
Mögliches Duplikat von [Was ist ein undefinierter Verweis/ungelöster externer Symbolfehler und wie behebe ich ihn?] (Http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-) symbol-error-and-how-do-i-fix) –
Die deprecation-Warnungen treten auf, weil Sie 'char const *' anstelle von 'char *' verwenden sollten. –