"undefined reference" to a template class function - c++

I am writing a template class for an array of objects, call it arrayobjclass, which holds pointers to other objects, specifically to other arrays in my implementation. The arrays are implemented as objects as well, call them arrayclass.
Looking for compilation ready with minimal changes.
when I try to test my classes with the following line,
g++ main.cpp arrayclass.cpp arrayobjclass.cpp -o arrayobj
I get the following error:
/tmp/ccEpROXj.o(.text+0x17c): In function `main':
: undefined reference to `arrayobjclass<arrayclass, int>::arrayobjclass(int)'
/tmp/ccEpROXj.o(.text+0x1dc): In function `main':
: undefined reference to `arrayobjclass<arrayclass, int>::addelem(arrayclass*)'
collect2: ld returned 1 exit status
I really can't understand what is wrong. any help would be appreciated. the short relevant part of the code is below if it helps. THANKS IN ADVANCE!
This is what i have in main:
#include "arrayclass.h"
#include "arrayobjclass.h"
#include <iostream>
// 5 arrays of 10 maxsize each
#define MAXSIZE_array 10
#define NUMB_objs 5
using namespace std;
int main () {
//create a simple array as an arrayclass object
arrayclass * numbers1 = new arrayclass (MAXSIZE_array);
//array of objects to hold pointers to simple arrays as created above
arrayobjclass<arrayclass,int> * myobjs = new arrayobjclass<arrayclass,int> (NUMB_objs);
//fill up the simple array
int i;
for (i=0; i<10; i++) {
numbers1->addelem(i);
}
//add a pointer to the simple array in my array of objects
myobjs->addelem(numbers1);
}
//arrayobjclass.h
//declarations of an array of pointers to objects
template <class obj, class key>
class arrayobjclass {
private:
//obj * arrayptr;
obj * objarray [];
int maxsize;
int totalelem;
public:
arrayobjclass(int);
bool addelem(obj *);
};
//arrayobjclass.cpp
//implementation of arrayobjclass, array of pointers to objects
#include "arrayobjclass.h"
#include "arrayclass.h"
template <class obj,class key>
arrayobjclass<obj,key>::arrayobjclass (int size){
maxsize=size;
objarray = new obj[maxsize];
totalelem = 0;
}
template <class obj, class key>
bool arrayobjclass<obj,key>::addelem (obj * newobj) {
if (totalelem < maxsize ) {
objarray[totalelem] = newobj;
totalelem ++;
return true;
}
return false;
}
//arrayclass.h
class arrayclass {
private:
int * arrayptr;
int maxsize;
int totalelem;
public:
arrayclass(int);
bool addelem(int);
};
//arrayclass.cpp
#include "arrayclass.h"
arrayclass::arrayclass (int size){
maxsize=size;
arrayptr = new int[maxsize];
totalelem = 0;
}
bool arrayclass::addelem (int addval) {
if (totalelem < maxsize ) {
arrayptr[totalelem] = addval;
totalelem ++;
return true;
}
return false;
}

You can't put template declarations in .cpp files like that. Template declarations and implementation need to be visible in the same translation unit. Put template implementations in headers that you #include directly.

Define your function templates in the header. Compiler needs to see them.
Cheers & hth.,

Because templates are compiled when required, this forces a
restriction for multi-file projects: the implementation (definition)
of a template class or function must be in the same file as its
declaration. That means that we cannot separate the interface in a
separate header file, and that we must include both interface and
implementation in any file that uses the templates.
From http://www.cplusplus.com/doc/tutorial/templates/

For anyone passing by
you can also #include the implementation files in main
in main:
#include "arrayobjclass.h"
#include "arrayclass.h"
#include "arrayobjclass.cpp"
#include "arrayclass.cpp"

Related

Not all of my constructors are being imported?

I'm making a heap class to be importable with heap.h and my constructors including bool types do not work, yet every other constructor and function imported works.
Here is what's in heap.h:
#ifndef __HEAP_INCLUDED__
#define __HEAP_INCLUDED__
#include <iostream>
#include <vector>
using namespace std;
class heap{
int capacity;
bool isMinHeap; //1 is min heap -- ascending order
vector<int> * content;
public:
heap();
heap(bool t);
heap(vector<int> * input);
heap(vector<int> * input, bool t);
void print();
void prettyPrint();
int parent(int i);
int leftChild(int i);
int rightChild(int i);
int size();
int getMax();
void insert(int data);
void heapifyDown(int index);
void heapifyUp(int index);
int invalidChild(int index);
int deleteMax();
int deleteMin();
bool minDir();
int at(int index);
};
vector<int> * heapSort(vector<int> * input);
void swap(vector<int> * vec, int a, int b);
#endif
Here are the defined constructors in heap.cpp. Note, all constructors work fine when I add a main to this file to test stuff:
class heap{
vector<int> * content;
int capacity = 256;
bool isMinHeap; //1 is min heap -- ascending order
public:
heap(){
content = new vector<int>;
isMinHeap = 0;
}
heap(bool t){
content = new vector<int>;
isMinHeap = t;
}
heap(vector<int> * input){
content = input;
isMinHeap = true;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
heap(vector<int> * input, bool t){
content = input;
isMinHeap = t;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
//other functions below
}
The constructors with bool do not work in main.cpp, which has #include "heap.h" at the top. The files are all in the same directory and I am compiling with this command: g++ heap.cpp main.cpp -o main. Why do two of my constructors not work?
The error I see is
/usr/bin/ld: /tmp/ccwomODk.o: in function `main':
main.cpp:(.text+0x4e2): undefined reference to `heap::heap(bool)'
collect2: error: ld returned 1 exit status
-Wall does not elaborate on the issue. I'm pretty sure the issue is with my linking somewhere because the constructors work inside of heap.cpp when I use them in there.
What you are doing with the class in the .cpp file is wrong. You are not allowed to define the class twice. There must only be one class heap { /*...*/ }; in the program (but it may be included in multiple .cpp files). Otherwise the one-definition-rule (ODR) is violated and the program has undefined behavior.
So remove everything you are showing from heap.cpp.
To define the constructors of heap in the heap.cpp file, you need to use this syntax:
#include "heap.h"
heap::heap() {
/*...*/
}
heap::heap(bool t) {
/*...*/
}
//...
and so on. The other member functions must be defined in a similar way, e.g.:
void heap::print() {
/*...*/
}
Furthermore, if you want to have a default member initializer as in
int capacity = 256;
add it in the declaration in the .h file instead.
I also want to add that having a pointer-to-std::vector as member is almost surely a wrong approach as well, but out-of-scope for the question.
When you declare a program element such as a class, function, or
variable, its name can only be "seen" and used in certain parts of
your program. The context in which a name is visible is called its
scope. For example, if you declare a variable x within a function, x
is only visible within that function body.
It seems you broke ODR rule so bad. Your class members including constructors has no body declared in the source file(heap.cpp).
Use '::' to make class members have a body:
//heap.cpp
"heap.h"
heap::heap()
{
}
heap:heap(vector<int> * input, bool t)
{
}
int heap::parent(int i)
{
return i;
}
// this is how you create a body for function that are class members
// the same should be done for all other functions

invalid pointer when using strings for generic class

So this is for my data structures class and I am struggling to understand why I am getting the error: invalid pointer: 0x00000000023ce048 ***. It only happens when I define my generic class using strings: NSequence<string> v3(10);
It should be noted that using int or even a different class worked fine. However, as soon as I write the line: NSequence<string> v3(10); I get the pointer error and I have no idea why?
UPDATE:
I've found that it was actually my destructor class that was causing the error. However, I now have no idea why this would cause an error specifically with string?
template<typename T>
NSequence<T>::~NSequence()
{
delete items;
}
Here is the definition of my class:
#include <algorithm>
#include <iostream>
#include <utility>
/* XXX: Implement all member functions for NSequence in NSequence.hpp */
template <typename T>
class NSequence
{
public:
explicit NSequence( int initSize = 0 );
private:
int numOfItems;
int totalCapacity;
T * items;
};
#include "NSequence.hpp" // do not change this line
#endif
Here is my constructor function:
template<typename T>
NSequence<T>::NSequence(int initSize)
{
if(initSize==0)
initSize=1;
numOfItems = initSize;
totalCapacity = initSize;
items = new T[totalCapacity];
}
Any advice would be greatly appreciated!

How can I get rid of the <error-type> type that is appearing in my method template? Both the input to the method and the class are templates

I am getting the error
declaration is incompatible with "void spectrogram<T>::update(<error-type> x)
I don't see any difference between the declaration and the definition of the method, not sure why it is complaining about just this one definition and not the constructor or destructor.
Here is vComplex.hpp
#ifndef VCOMPLEX_H
#define VCOMPLEX_H
template <class T>
class vComplex {
public:
T* realp;
T* imagp;
int length; // for bookkeeping
vComplex(void) { }
vComplex (T* I, T* Q, int len) {
realp = I;
imagp = Q;
length = len;
}
~vComplex(void) {
free(realp);
free(imagp);
}
void put(T* I, T*Q, int len) {
realp = I;
imagp = Q;
length = len;
}
};
#endif
the function declaration for update in spectrogram.hpp, with other members removed:
#ifndef SPECTROGRAM_H
#define SPECTROGRAM_H
template <typename T>
class spectrogram {
public:
void update(vComplex<T> x);
};
#endif
and the function signature (and includes) for update in spectrogram.cpp:
#include <stdio.h>
#include <math.h>
#include "spectrogram.hpp"
#include "vComplex.hpp"
template <typename T>
void spectrogram<T>::update(vComplex<T> x) {
//do stuff
}
In VS 2017, I get the red underline under update and everything inside of it breaks basically. VS is saying T is undefined which I'm assuming is caused by the overall error. I have to use dynamically allocated pointers, I don't have the option of using other types or containers.

Aggregates, templating, and dynamic vectors

I keep getting 3 errors. They're all related to the way I'm aggregating templates I'm assuming but i can't find anything to help me figure this out. My teacher wasn't super clear on how we're supposed to get the output he wants us to get.
In file included from main.cpp:10:
./Table.h:15:9: error: use of class template 'RowAray' requires template arguments
Here is what i wrote
RowAray.cpp
#ifndef ROWARAY_H // if constant ROWARAY_H not defined do not execute
#define ROWARAY_H // defines constant ROWARAY_H
#include <iostream>
#include <new> // Needed for bad_alloc exception
#include <cstdlib> // Needed for the exit function
template <class T>
class RowAray{
private:
int size;
T *rowData;
void memError(); // Handles memory allocation errors
void subError(); // Handles subscripts out of range
public:
RowAray(T); //used to construct row Array object
~RowAray(){delete [] rowData;} //used to deallocate dynamically allocated memory from Row array
int getSize(){return size;} //inline accessor member function used to return length of Row array
T getData(int i){return (( i >=0&& i < size)?rowData[i]:0);} //
T &operator[](const int &);
};
template <class T>
RowAray<T>::RowAray(T colSize){
size =colSize>1?colSize:1;
// Allocate memory for the array.
try
{
rowData = new T [size];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array.
for (int count = 0; count < size; count++)
*(rowData + count) = rand()%90+10;
}
template <class T>
void RowAray<T>::memError()
{
cout << "ERROR:Cannot allocate memory.\n";
exit(EXIT_FAILURE);
}
template <class T>
void RowAray<T>::subError()
{
cout << "ERROR: Subscript out of range.\n";
exit(EXIT_FAILURE);
}
template <class T>
T &RowAray<T>::operator[](const int &sub)
{
if (sub < 0 || sub >= size)
subError();
else
return rowData[sub];
}
#endif /* ROWARAY_H */
Table.cpp
#ifndef TABLE_H
#define TABLE_H
#include "RowAray.h"
template <class T>
class Table{
private:
int szRow;
RowAray **records;
public:
Table(int,int); //used to construct Table object
~Table(); //used to deallocate dynamically allocated memory from Table object
int getSzRow(){return szRow;} //used to return row size
int getSize(int row){return records[row>=0?row:0]->getSize();} //used to return column size
T getRec(int, int); //used to return inserted random numbers of 2d arrays
};
template <class T>
Table<T>::Table(int r, int c ){
//Set the row size
this->szRow = r;
//Declare the record array
records = new RowAray*[this->szRow];
//Size each row
int allCol = c;
//Create the record arrays
for(int i=0;i<this->szRow;i++){
records[i]=new RowAray(allCol);
}
}
template <class T>
T Table<T>::getRec(int row, int col){
//if else statement used to return randomly generated numbers of array
if(row >= 0 && row < this->szRow && col >= 0 && col < records[row]->getSize()){
return records[row]->getData(col);
}else{
return 0;
}
}
template <class T>
Table<T>::~Table(){
//Delete each record
for(int i=0;i<this->szRow;i++){
delete records[i];
}
delete []records;
}
#endif /* TABLE_H */
main.cpp
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//User Libraries
#include "RowAray.h"
#include "Table.h"
//Global Constants
//Function Prototype
template<class T>
void prntRow(T *,int);
template<class T>
void prntTab(const Table<T> &);
//Execution Begins Here!
int main(int argc, char** argv) {
//Initialize the random seed
srand(static_cast<unsigned int>(time(0)));
//Declare Variables
int rows=3,cols=4;
//Test out the Row with integers and floats
RowAray<int> a(3);
RowAray<float> b(4);
cout<<"Test the Integer Row "<<endl;
prntRow(&a,3);
cout<<"Test the Float Row "<<endl;
prntRow(&b,4);
//Test out the Table with a float
Table<float> tab1(rows,cols);
Table<float> tab2(tab1);
//Table<float> tab3=tab1+tab2;
cout<<"Float Table 3 size is [row,col] = Table 1 + Table 2 ["
<<rows<<","<<cols<<"]";
//prntTab(tab3);
//Exit Stage Right
return 0;
}
template<class T>
void prntRow(T *a,int perLine){
cout<<fixed<<setprecision(1)<<showpoint<<endl;
for(int i=0;i<a->getSize();i++){
cout<<a->getData(i)<<" ";
if(i%perLine==(perLine-1))cout<<endl;
}
cout<<endl;
}
template<class T>
void prntTab(const Table<T> &a){
cout<<fixed<<setprecision(1)<<showpoint<<endl;
for(int row=0;row<a.getSzRow();row++){
for(int col=0;col<a.getSize();col++){
cout<<setw(8)<<a.getRec(row,col);
}
cout<<endl;
}
cout<<endl;
}
"RowAray" is a template, with one template parameter:
template <class T>
class RowAray
See there? It's a template, one template parameter.
Now, over here:
template <class T>
class Table{
private:
int szRow;
RowAray **records;
See there? No template parameter when referring to the RowAray template, here. When using a template, its parameters must also be specified (unless they have a default, which is irrelevant here).
The fact that, here, you are defining a new template, Table, with one template parameter -- that's irrelevant.
You probably intended to use
RowAray<T> **records;
here; but that's just based on a cursory look at this pile of code so don't automatically take my word for it. You need to figure out what you intended to do here, and specify the correct template parameter.
This is not the only parameter-less reference in the shown code. You need to find, and fix all of them.
Furthermore, you also dumped:
using namespace std;
before proceeding and #includeing a bunch of header files, including standard library header files. This is a bad programming practice, and often creates subtle, and difficult to figure out compilation errors, if not outright wrong code. You must get rid of using namespace std; as well, especially when a bunch of #includes are involved.

why i get {error :2061 identifier {ctor

In header file:
#ifndef Array_h
#define Array_h
#include "stdafx.h"
using namespace std;
template<class T>
class Arrayc
{
private:
int Arraysize;
int length;
T *array;
public:
Arrayc(int size);
~Arrayc();
};
template<class T>
Arrayc<T>::Arrayc(int size)
{
Arraysize = size;
length = 0;
array = new T[Arraysize];
}
#endif
In main source file:
Arrayc<int> *Arrayofintegers;
Arrayc<float> *Arrayoffloat;
// These lines have the error
Arrayofintegers = new Arrayc<int>::Arrayc(10);
Arrayoffloat = new Arrayc<float>::Arrayc(5);
You only need specify the scoped name, Arrayc<T>::Arrayc, when defining the constructor.
To call the constructor, simply use Arrayc<T>(/*args*/).
Of course, you really don't even need new at all, but that's unrelated to the error.
You are not passing any clues to your constructors of what you want T to be at the time that you use them. I'd suggest adding a parameter T to your constructor, even if you only use it as a phony initializer value, so that the compiler can deduce what type Arrayc() will be, e.g., new Arrayc(10, 0) or new Arrayc (10, 0.0f)