Here the code
#include <iostream>
#include <conio.h>
using namespace std;
template <typename T> class grid
{
public:
grid();
~grid();
void createCells();
private:
T **cells;
};
int main(int argc, char **argv)
{
grid<int> intGrid;
_getch();
return 0;
}
While trying to compile - got a message:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
grid<int>::~grid<int>(void)" (??1?$grid#H##QAE#XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
grid<int>::grid<int>(void)" (??0?$grid#H##QAE#XZ) referenced in function _main
What need to do?
You need to define the constructor and destructor (you just declared them):
template <typename T> class grid
{
public:
grid()
{} // here
~grid()
{} // and here
void createCells();
private:
T **cells;
};
Related
I'm having strange linkage problems with the following very simple application, which has a class inheriting both QObject and an interface class.
#include <QApplication>
#include <memory>
#include <iostream>
#include <qobject>
class IFoo {
public:
virtual ~IFoo() {}
virtual void foo()=0;
};
class Foo: public QObject, public IFoo
{
Q_OBJECT
public:
explicit Foo(QObject *parent=0): QObject(parent) { std::cout << "foo ctor" << std::endl; }
void foo() override { std::cout << "foo::foo" << std::endl; }
};
std::unique_ptr<IFoo> createFoo() { return std::unique_ptr<IFoo>(new Foo()); }
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto foo = createFoo();
return a.exec();
}
This causes the following errors:
LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Foo::metaObject(void)const " (?metaObject#Foo##UEBAPEBUQMetaObject##XZ)
LNK2001: unresolved external symbol "public: virtual void * __cdecl Foo::qt_metacast(char const *)" (?qt_metacast#Foo##UEAAPEAXPEBD#Z)
LNK2001: unresolved external symbol "public: virtual int __cdecl Foo::qt_metacall(enum QMetaObject::Call,int,void * *)" qt_metacall#Foo##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
LNK1120: 3 unresolved externals
I have obviously tried run qmake, clean and rebuild without effect. If I make the class Foo a plain C++ class by removing all Qt references, it works correctly.
What could be wrong?
I'm trying to call a public member function:
// main.cpp
#include "AH.h"
int somefunc()
{
//...
// it - object of class A
it.set_mean(angle, mean);
//...
}
that declared in .h
// AH.h
class A
{
//...
public:
inline void set_mean(const int angle, const double mean_value);
//...
}
and defined in .cpp file:
// A.cpp
#include "AH.h"
inline void A::set_mean(const int angle, const double mean_value)
{
mean[angle] = mean_value;
}
and recieving an LNK error:
error LNK2019: unresolved external symbol "public: void __cdecl A::set_mean(int,double)" (?set_mean#A##QEAAXHN#Z) referenced in function "int __cdecl somefunc(class std::vector<class A,class std::allocator<class A> > &)" (?somefunc##YAHAEAV?$vector#VA##V?$allocator#VA###std###std###Z)
why it's happening?
I am trying to implement a PointArray class derived from a template. Here is what my hpp file for PointArray looks like:
#ifndef POINTARRAY_H
#define POINTARRAY_H
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "Array.hpp"
using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;
namespace Abhishek
{
namespace CONTAINERS
{
class PointArray : public Array<Point>
{
public:
PointArray();//Default constrcutor.
PointArray(int size);//Constructor with size argument.
PointArray(const PointArray& arr);//Copy constructor.
~PointArray();//Destructor.
double Length() const;//Length function.
};
}
}
#endif
My cpp looks like this :
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "PointArray.hpp"
using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;
namespace Abhishek
{
namespace CONTAINERS
{
//Default constructor.
PointArray::PointArray(): Array<Point>()
{
cout<<"Point arr default cons"<<endl;
}
//Constructor with size argument.
PointArray::PointArray(int size) : Array<Point>(size)
{
}
//Copy constructor.
PointArray::PointArray(const PointArray& arr) : Array<Point>(arr)
{
}
//destrcutor.
PointArray::~PointArray()
{
}
}
}
I get the LNK error :
error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(void)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#XZ) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(void)" (??0PointArray#CONTAINERS#Abhishek##QAE#XZ)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(int)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#H#Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(int)" (??0PointArray#CONTAINERS#Abhishek##QAE#H#Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(class Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point> const &)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#ABV012##Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(class Abhishek::CONTAINERS::PointArray const &)" (??0PointArray#CONTAINERS#Abhishek##QAE#ABV012##Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::~Array<class Abhishek::CAD::Point>(void)" (??1?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#XZ) referenced in function __unwindfunclet$??0PointArray#CONTAINERS#Abhishek##QAE#XZ$0
1>C:\Users\Rambo\Documents\Level 6\Section 4.2b\Exercise 3\Debug\Exercise 3.exe : fatal error LNK1120: 4 unresolved externals
I dont understand why this could be happening. I included all the relevant header files and CPP files. If anyone can help I will really appreciate it.
You forgot to post the most relevant header: the one that declares the class template that causes the error. Almost certainly, that header declares the default constructor of the Array template:
Array();
but doesn't define it; either there is no definition, or you have a definition in a source file.
In either case, you'll get an error since templates must be defined in any translation unit that uses them. This means that you'll need to define the constructor (and any other member functions) in the header, to include them wherever they are used.
If that's not the problem, then please post the header so we can investigate further.
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::getfirst(int &)" (?getfirst#?$LinkedSortedList#H##UAE_NAAH#Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::clear(void)" (?clear#?$LinkedSortedList#H##UAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::print(void)const " (?print#?$LinkedSortedList#H##UBEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::insert(int)" (?insert#?$LinkedSortedList#H##UAE_NH#Z) referenced in function _main
1>main.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::find(int)const " (?find#?$LinkedSortedList#H##UBE_NH#Z)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall LinkedSortedList<int>::size(void)const " (?size#?$LinkedSortedList#H##UBEHXZ)
1>c:\users\chris\documents\visual studio 2010\Projects\lab0\Debug\lab0.exe : fatal error LNK1120: 6 unresolved externals
This is what I recieve when trying to compile my code. I've narrowed it down to (i believe) this section of code here:
#ifndef _LinkedSortedListClass_
#define _LinkedSortedListClass_
#include "LinkedNode.h"
#include "SortedList.h"
template <class Elm>
class LinkedSortedList: public SortedList<int> {
public:
void clear();
bool insert(Elm newvalue);
bool getfirst(Elm &returnvalue);
void print() const;
bool find(Elm searchvalue) const;
int size() const;
private:
LinkedNode<Elm>* head;
};
#endif
This is the child class of the SortedList, which is this, in case it's needed..
#ifndef _SortedListClass_
#define _SortedListClass_
template <class Elm> class SortedList {
public:
// -------------------------------------------------------------------
// Pure virtual functions -- you must implement each of the following
// functions in your implementation:
// -------------------------------------------------------------------
// Clear the list. Free any dynamic storage.
virtual void clear() = 0;
// Insert a value into the list. Return true if successful, false
// if failure.
virtual bool insert(Elm newvalue) = 0;
// Get AND DELETE the first element of the list, placing it into the
// return variable "value". If the list is empty, return false, otherwise
// return true.
virtual bool getfirst(Elm &returnvalue) = 0;
// Print out the entire list to cout. Print an appropriate message
// if the list is empty. Note: the "const" keyword indicates that
// this function cannot change the contents of the list.
virtual void print() const = 0;
// Check to see if "value" is in the list. If it is found in the list,
// return true, otherwise return false. Like print(), this function is
// declared with the "const" keyword, and so cannot change the contents
// of the list.
virtual bool find(Elm searchvalue) const = 0;
// Return the number of items in the list
virtual int size() const = 0;
};
#endif
Thanks so much for any help; our last class taught us nothing of inheritance, but this is project #1 for this class, without being taught inheritance here either, so this is all touch and go for me, despite what I managed to look up on Google.
Your methods aren't defined. So the linker is complaining because it can't link to their definitions.
Maybe it helps if you placed the definitions of your functions in your header file. This makes it easier for the compiler to resolve these external symbols.
I hope this will help.
Regards,
Philinator
#include <iostream>
using namespace std;
class A {
public:
void function( int num);
bool function1()const;
virtual bool function2() const=0;
};
class B:public A {
public :
bool function2()const;
};
int _tmain(int argc, _TCHAR* argv[])
{
void (A::* p)(int)= &A::function; //不是地址,而是一个指向成员函数的指针
// Edit: Google translation of the above comment is
// "Not address, but a pointer to a member function pointer"
bool (A::* p1)()const =&A::function1; // 指向成员函数的指针可以指向一个常量成员函数
// Edit: Google translation of the above comment is
// "Point to a member function pointer can point to a const member function"
B b;
A *a=&b;
(a->*p1)();
(b.*p1)();
return 0;
}
but when I link it:
1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " (?function1#A##QBE_NXZ) referenced in function _wmain
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" (?function#A##QAEXH#Z) referenced in function _wmain
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " (?function2#B##UBE_NXZ)
can you tell me why?
You haven't implemented A::function(), A::function1(), or B::function2(). You need to do that.
A::function1, A::function and B::function2 are all declared, but never defined. You can't get a pointer to the function if it is not defined, where would it point?