-1

I'm (somewhat) new to C++ and I've been trying to get a program to work. It all compiles fine until I start to define a class constructor within the source file, even though I have declared it in the header file. I'm not sure if it's because I haven't defined all of my methods, because this is my second attempt at defining them (I'm literally rewriting them slowly to see if it helps).

The code in question is below (this is what is on the cpp file):

#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;

}

The output I receive is below:

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)

And just in case people need it, this is the class in question:

class Diode : public StockItem {

    public:       

        Diode(char* componentT, char* code, int quantity, int price);

        Diode(const Diode &D);

        Diode& operator=(const Diode& D);

        ~Diode();

};

My question is why is this effecting the compilation? Any feedback (regardless of relevance) is greatly appreciated, thank you.

  • Have you looked at http://stackoverflow.com/questions/3065154/undefined-reference-to-vtable? – NPE Apr 01 '16 at 01:41
  • 2
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Sam Varshavchik Apr 01 '16 at 01:44
  • The deprecation warnings are happening because you should be using `char const*` instead of `char*`. – Edward Strange Apr 01 '16 at 01:47
  • 2
    Instead of monkeying around with pointers, arrays of char, and `strcpy()`, you might want to use `std::string` (from ``). Hugely less error prone. – Peter Apr 01 '16 at 01:53
  • The errors mention the `StockItem` class several times as the source of the errors and it doesn't occur to you to actually post any of the `StockItem` class? – Michael Burr Apr 01 '16 at 02:10
  • You failed to define bodies for `StockItem::getComponentInfo()` and `StockItem::setComponentInfo(char*)`. I'm guessing those are virtual functions – M.M Apr 01 '16 at 02:18
  • 1
    Please post a [MCVE]. Minimal code that actually reproduces the error you have a problem with and no other errors and no other code required or provided. – Yakk - Adam Nevraumont Apr 01 '16 at 02:24

1 Answers1

1

Error comes from undefined reference when linking. You declare the functions, use them, but never implement the functions. When linker link the object files and lib files, it would check the export symbols and import symbols then match them together.

Obvious, in your case, other class use your functions, treated as import symbols, but in class's object file, doesn't contain the function in export symbols table.

Tongxuan Liu
  • 270
  • 1
  • 12