My question is that, I have a template class template<class T> AList as base, and I wanna get a derived class from the template, i.e. get class BList: public AList<mydefinedtype> without much modification.
alist.h
#ifndef alist_h
#define alist_h
template<class T> class AList
{
public:
AList(){
arr = new T[20];
numitems = 0;
};
void append(T value);
private:
T *arr;
int numitems;
};
#endif /* alist_h */
alist.cpp
#include "alist.h"
template<class T> void AList<T>::append(T value)
{
arr[numitems] = value;
++numitems;
return;
}
blist.h
#include "alist.cpp"
#include <string>
using namespace std;
typedef struct
{
string a, b;
int key;
} record;
class BList: public AList<record>{
public:
void test(void){
cout << "this is from BList" << endl;
}
};
blist.cpp
#include "blist.h"
main.cpp
#include <iostream>
#include "blist.cpp"
using namespace std;
int main(){
record testRecord[3];
testRecord[0] = {"Mark", "A", 1};
testRecord[1] = {"Anla", "B", 2};
testRecord[2] = {"Cindy", "C", 3};
BList blist = BList();
for(auto i: testRecord){
// blist.append(i); // will compile error
blist.test();
}
return 0;
}
It will fail as follows, I wonder how to compile or how to fix the bug.
error info
Undefined symbols for architecture x86_64:
"AList<record>::append(s)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
Not sure where comes from the issue.
// Example program
#include <iostream>
#include <string>
struct record{
int a;
};
template<class T>
class AList{
public:
AList()=default;
void append(T value){}
};
template<class T>
class BList:public AList<T>{
public:
void test(void){}
};
int main()
{
BList<record> blist;
record recordarr[3] ;
// some initialization
for(auto i:recordarr){
blist.append(i);
blist.test();
}
}
The problem you have is that the AList() constructor, append(T) and test() are only declared but not defined. The above code should compile.
You should put your template classes entirely in header files. See this question and this C++ FAQ for details on why.
You should also never #include .cpp files. You should only ever #include header files.
Below I have your code after the required modifications to make it compile. I also removed your memory leak.
alist.h:
#ifndef alist_h
#define alist_h
template<class T> class AList {
public:
AList() {
arr = new T[20];
numitems = 0;
};
~AList() {
delete[] arr;
}
void append(T value) {
arr[numitems] = value;
++numitems;
}
private:
T *arr;
int numitems;
};
#endif /* alist_h */
blist.h:
#ifndef blist_h
#define blist_h
#include "alist.h"
#include <string>
using namespace std;
typedef struct {
string a, b;
int key;
} record;
class BList: public AList<record> {
public:
void test(void) {
cout << "this is from BList" << endl;
}
};
#endif /* blist_h */
main.cpp:
#include <iostream>
#include "blist.h"
using namespace std;
int main() {
record testRecord[3];
testRecord[0] = {"Mark", "A", 1};
testRecord[1] = {"Anla", "B", 2};
testRecord[2] = {"Cindy", "C", 3};
BList blist = BList();
for (auto i: testRecord) {
blist.append(i);
blist.test();
}
return 0;
}
Summary of changes
I made the following changes:
Moved body of AList::append into alist.h, and deleted alist.cpp
Added AList destructor to free the dynamically allocated memory allocated in AList::AList
In blist.h, included alist.h instead of alist.cpp
Deleted blist.cpp
In main.cpp, included blist.h instead of blist.cpp
Related
I am in the process of constructing a StackArray. I already have a "Stack.h" implemented with a constructor. I was wondering what I would do in my "StackArray.h" file to use the Stack.h file. I was thinking to use inheritance but it was giving me a error.
My code is as follows:
Array.h
#include <iostream>
#include <assert.h>
using namespace std;
#ifndef _ARRAY_H
#define _ARRAY_H
template<class T>
class Array{
private:
T *a;
int length;
public:
// constructor
Array (int len){
length = len;
a = new T[length];
for (int i = 0; i < len; i++){
a[i]=0;
}
}
// destructor
~Array()
{delete[] a;}
// operator overload
T& operator [](int i){
assert (i>=0 && i < length);
return a[i];
}
//get the length of the array
int arraylength(){
return length;
}
};
#endif
ArrayStack.h
#ifndef _ARRAYSTACK_H_
#define _ARRAYSTACK_H_
#include "Array.h"
using namespace std;
template<class T>
class ArrayStack
{
protected:
Array<T> a;
int n;
public:
ArrayStack(int len);
virtual ~ArrayStack();
};
template<class T>
ArrayStack<T>::ArrayStack(int len){
// I don't know what to do here to implemented constructor from another class.
}
#endif
Any suggestion would be great, Thank you
Andy
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I meet a problem when constructing a class. class "Graph" import a class "Bag" in another file and use "Bag" as its component.
//Graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <fstream>
#include <iostream>
#include <vector>
#include "Bag.h"
class Bag;
class Graph
{
public:
Graph(int V);
Graph(std::ifstream& in_file);
int getV() { return V; }
int getE() { return E; }
void addEdge(int v, int w);
void showadj() ;
private:
int V;
int E;
std::vector<Bag> adj;
};
#endif
And "Bag.h" is as follow:
//Bag.h
#ifndef BAG_H
#define BAG_H
#include <vector>
#include <iostream>
class Bag
{
public:
Bag();
void addBag(int i) { content.push_back(i); }
void showBag();
private:
std::vector<int> content;
};
#endif
Graph.cpp:
//Graph.cpp
#include "Graph.h"
#include "Bag.h"
Graph::Graph(int V) : V(V), E(0)
{
for (int i = 0; i < V; i++)
{
Bag bag;
adj.push_back(bag);
}
}
Bag.cpp(sorry, forget it):
#include "Bag.h"
void Bag::showBag()
{
for (int i : content)
{
std::cout << i << " ";
}
}
When I try to complie these two classes, an error comes out saying:
C:\Users\ADMINI~1\AppData\Local\Temp\ccMj4Ybn.o:newtest.cpp:(.text+0x1a2): undef
ined reference to `Bag::Bag()'
collect2.exe: error: ld returned 1 exit status
You need to also implement your constructor Bag::Bag() as it is missing in your Bag.cpp file.
This is what the error tells you. If you don't need the constructor, then you should remove it from your class definition, which would also solve this error.
An alternative would be to provide an empty constructor in your Bag.h file
class Bag
{
public:
Bag() {}
...
}
I am getting the following error:
'class name' does not name a type for all of my classes.
I suspect it may be a circular dependency but I have no clue how to solve it as each class requires access to a function from the next. Below are my classes:
Container.h:
#ifndef CONTAINER_H
#define CONTAINER_H
#include "Factory.h"
class Container
{
public:
Container()
{
array = new int[10];
for (int i = 0; i < 10; ++i) {
array[i] = i;
}
}
Iterator* createIterator()
{
Factory fac;
return fac.factoryMethod();
}
friend class Iterator;
private:
int* array;
};
#endif //CONTAINER_H
Factory.h:
#ifndef FACTORY_H
#define FACTORY_H
#include "Iterator.h";
class Factory
{
Iterator* factoryMethod(Container* con)
{
return new Iterator(con);
}
};
#endif //FACTORY_H
Iterator.h:
#ifndef ITERATOR_H
#define ITERATOR_H
#include "Container.h"
class Iterator
{
public:
Iterator(Container* con)
{
this->con =con;
}
int getFromIndex(int i)
{
return con->array[i];
}
private:
Container* con;
};
#endif //ITERATOR_H
main.cpp:
#include <iostream>
using namespace std;
#include "Container.h"
#include "Iterator.h"
int main() {
Container con;
Iterator* it = con.createIterator();
cout<<it->getFromIndex(2)<<endl;
return 0;
}
Thank you in advance for any help.
It is indeed a circular dependency between your headers. Container.h includes Factory.h, which includes Iterator.h, which includes Container.h.
The solution is to move the implementations of member functions from header files into source files. That way, header files will only need declarations, not definitions, of the classes, which you can easily put directly in the "consuming" header files:
class Iterator;
class Container
{
public:
Container();
Iterator* createIterator();
friend class Iterator;
private:
int* array;
};
Then, in an appropriate source file (such as Container.cpp), implement the member functions and include any headers you need:
Container.cpp
#include "Container.h"
#include "Factory.h"
Container::Container() : array(new int[10])
{
for (int i = 0; i < 10; ++i) {
array[i] = i;
}
}
Iterator* Container::createIterator()
{
Factory fac;
return fac.factoryMethod();
}
(Dtto for Factory and Iterator, of course).
Don't forget to link all the source files together when building your final binary.
I have simple class in a header file: a.hh
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
a()
{
i = 0;
}
};
#endif
Then i have a file:b.cc
#include <iostream>
#include "a.hh"
using namespace std;
int main(int argc, char** argv)
{
a obj;
obj.i = 10;
cout << obj.i << endl;
return 0;
}
>
Till this point everything is fine.
I compile the code and it compiles fine.
But as soon as i add a vector in the class:
#ifndef a_hh
#define a_hh
class a
{
public:
int i;
vector < int > x;
a()
{
i = 0;
}
};
#endif
I get a compilation error as below:
> CC b.cc
"a.hh", line 7: Error: A class template name was expected instead of vector.
1 Error(s) detected.
What is the problem with declaring a vector here as a member?
You need to #include <vector> and use the qualified name std::vector<int> x;:
#ifndef a_hh
#define a_hh
#include <vector>
class a{
public:
int i;
std::vector<int> x;
a() // or using initializer list: a() : i(0) {}
{
i=0;
}
};
#endif
Other points:
(as commented by EitanT) I removed the additional qualification a:: on the constructor
have a read of Why is "using namespace std" considered bad practice?
declaring a vector as a class member:
#include <iostream>
#include <vector>
using namespace std;
class class_object
{
public:
class_object() : vector_class_member() {};
void class_object::add_element(int a)
{
vector_class_member.push_back(a);
}
void class_object::get_element()
{
for(int x=0; x<vector_class_member.size(); x++)
{
cout<<vector_class_member[x]<<" \n";
};
cout<<" \n";
}
private:
vector<int> vector_class_member;
vector<int>::iterator Iter;
};
int main()
{
class_object class_object_instance;
class_object_instance.add_element(3);
class_object_instance.add_element(6);
class_object_instance.add_element(9);
class_object_instance.get_element();
return 0;
}
1.You need to #include <vector> and using namespace std, then a.hh just like below:
#ifndef a_hh
#define a_hh
#include <vector>
using namespace std;
class a
{
public:
int i;
vector <int> x;
a()
{
i = 0;
}
};
#endif
2. If you don't want to only use std namespace in all your code, you can specified the namespace before type, just like std::vector<int> x;
I got this error and I can't figure out why.
#include <vector>
#include <cstdlib>
#ifndef PERSONALVEC_HPP_
#define PERSONALVEC_HPP_
template <class T,class PrnT>
class PersonalVec
{
public:
PersonalVec() {}
~PersonalVec()
{
//TODO: delete vector.
}
void push_back(T& obj)
{
int index = rand()%_vec.size();
}
private:
vector<T*> _vec;
};
#endif /* PERSONALVEC_HPP_ */
Both rand and vector are in the std namespace.
Use
private:
std::vector<T*> _vec;
and
std::rand()
On this line:
int index = rand()%_vec.size();
You call the function rand() but do not include the header which declares it. Specifically, you need to add the following line to the top of your program:
#include <cstdlib>
Part of the problem is likely that you are using a vector without being in the std namespace. change vector<T*> _vec to std::vector<T*> _vec.
The following code (Ideone linky: http://www.ideone.com/HgL1e) seems to work fine.
#include <vector>
#include <cstdlib>
template <class T,class PrnT>
class PersonalVec
{
public:
PersonalVec() {}
~PersonalVec()
{
//TODO: delete vector.
}
void push_back(T& obj)
{
int index = rand()%_vec.size();
}
private:
std::vector<T*> _vec;
};
int main()
{
int i = 1;
PersonalVec<int, int> testVec;
testVec.push_back(i);
return 0;
}