That's my program written in C++ (OOP):
#include <iostream>
#include <string>
using namespace std;
class book {
string name,gvari;
double cost;
int year;
public:
book(){};
book(string a, string b, double c, int d) { a=name;b=gvari;c=cost;d=year; }
~book() {}
double setprice(double a) { return a=cost; }
friend ostream& operator <<(ostream& , book&);
void printbook(){
cout<<"wignis saxeli "<<name<<endl;
cout<<"wignis avtori "<<gvari<<endl;
cout<<"girebuleba "<<cost<<endl;
cout<<"weli "<<year<<endl;
}
};
ostream& operator <<(ostream& out, book& a){
out<<"wignis saxeli "<<a.name<<endl;
out<<"wignis avtori "<<a.gvari<<endl;
out<<"girebuleba "<<a.cost<<endl;
out<<"weli "<<a.year<<endl;
return out;
}
class library_card : public book {
string nomeri;
int raod;
public:
library_card(){};
library_card( string a, int b){a=nomeri;b=raod;}
~library_card();
void printcard(){
cout<<"katalogis nomeri "<<nomeri<<endl;
cout<<"gacemis raodenoba "<<raod<<endl;
}
friend ostream& operator <<(ostream& , library_card&);
};
ostream& operator <<(ostream& out, library_card& b) {
out<<"katalogis nomeri "<<b.nomeri<<endl;
out<<"gacemis raodenoba "<<b.raod<<endl;
return out;
}
int main() {
book A("robizon kruno","giorgi",15,1992);
library_card B("910CPP",123);
cout<<A;
cout<<B;
A.setprice(15);
cout<<B;
system("pause");
return 0;
}
//P.S.
//B.printcard();
//A.printbook();
// As I've overloaded the "<<" operators, I've removed these two lines and put "cout<<A" "cout<<B". I hope, I'm right. By the way, error isn't related to these ones.
Here is the error, it may help you:
1>Neyw.obj : error LNK2019: unresolved external symbol "public: __thiscall library_card::~library_card(void)" (??1library_card##QAE#XZ) referenced in function _main
1>c:\users\geo\documents\visual studio 2010\Projects\Newww\Debug\Newww.exe : fatal error LNK1120: 1 unresolved externals
1>Build FAILED.
The problem is I can't figure out what's wrong with the code.. There are two classes, the main one, book, and the successor class, library_card. Why does it give me such an error?
Replace
~library_card();
with
~library_card() {}
You haven't implemented the destructor and this is exactly what your linker is saying.
Or you can remove this string as well if you don't know why you need this destructor.
There's no function body for ~library_card();. Just remove that destructor, or populate it, whichever you like.
Whilst you're there, have a read about virtual destructors, and consider whether your classes need them.
Related
I have two classes : Individu and Cite and as u can see Individu is defined before
//file.hpp
#include <iostream>
#include <stdexcept>
#include <vector>
extern Cite CITE;
class Individu {
protected:
static int id;
TYPE t;
public:
Individu();
virtual ~Individu();
static int & getCompteur();
virtual void afficher(std::ostream& ) const;
virtual TYPE getType() const;
};
class Cite {
std::vector<const Individu *> tab;
public:
Cite();
~Cite();
void addPersonne(const Individu *);
int size() const;
};
std::ostream& operator<<(std::ostream&, const Individu& );
#endif
I need to add an Individu one it's instanciated to the tab vector of Cite and sisnce there is just one Cite I declared Exctern CITE Cite to work with just like that :
// file.cpp
#include <algorithm>
#include "file.hpp"
int Individu::id = 0;
Individu::Individu() {
CITE.addPersonne(*this);
id++;
}
Individu::~Individu(){
}
int& Individu::getCompteur() {
return id;
}
void Individu::afficher(std::ostream& o) const{
o << id;
}
void Personne::afficher(std::ostream& o) const {
o << nom << " " << id;
}
std::ostream& operator<<(std::ostream& o, const Individu& i ){
i.afficher(o);
return o;
}
TYPE Individu::getType() const {
throw IllegalException();
}
Cite::Cite(){
}
Cite::~Cite() {
}
void Cite::addPersonne(const Individu * i){
tab.push_back(i);
}
int Cite::size() const {
return tab.size();
}
and when I compile I got this error :
file.hpp:13:8: error: ‘Cite’ does not name a type
13 | extern Cite CITE;
| ^~~~
file.cpp: In constructor ‘Individu::Individu()’:
file.cpp:30:5: error: ‘CITE’ was not declared in this scope
30 | CITE.addPerconne(*this);
| ^~~~
make: *** [makefile:15 : build/deviant.o] Erreur 1
I understand that Cite is not yet defined so that's why I got that error , so hwo can I fix it ?
You have two issues in your code:
extern Cite CITE is declared before the class Cite is defined, so the compiler doesn't know what a Cite is at that point. You should move this declaration after the definition of Cite.
You never define CITE. An extern variable declaration is a promise to the compiler that you will define that variable later. You're essentially saying "I promise a Cite object named CITE exists even though you can't see it right now". You broke that promise by never actually creating that object. You need to define a Cite CITE somewhere (most likely in file.cpp).
This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Closed 3 years ago.
So I'm new to C++ and Visual Studio and I'm trying to implement a hash table using templates. I have four files: main.cpp, HashNode.h, HashTable.h, and HashTable.cpp.
main calls the HashTable constructor with a paramenter (the definition is in HashNode.h, with the implementation in the cpp file), but this throws 2 unresolved external errors: one for the called constructor, and one for what I assume to be the default constructor.
However, main also calls the HashNode constructor with no problems. HashNode has its implementation and declaration all in the HashNode.h file, but moving HashTable's implementation to its .h file didn't clear the error. So I'm very confused lol.
I'm running Visual Studio 2019, fresh install, and using the default build button to build it. It does compile and run other things (like hello world), just not this.
I've also tried adding random garbage into HashTable.cpp to see if the compiler just didn't see that it existed, but that's not the case. It also throws a compilation error then.
HashTable.h:
#pragma once
#include "HashNode.h"
template <typename T>
class HashTable
{
public:
void AddItem(int key, T item);
T* GetItem(int key);
HashTable(int buckets);
~HashTable();
int print();
private:
HashNode<T>** elements;
int buckets;
};
HashTable.cpp:
#include "HashTable.h"
#include "HashNode.h"
#include <stdexcept>
template<typename T>
HashTable<T>::HashTable(int buckets)
{
elements = new HashNode<T> * [buckets];
for (int i = 0; i < buckets; i++)
{
elements[i] = nullptr;
}
HashTable::buckets = buckets;
}
... //other methods defined below
HashNode.h
#pragma once
template <typename V>
class HashNode
{
public:
HashNode(int key, const V value) : k(key), v(value), next(nullptr) {}
int getKey () const { return k; }
V getValue() const { return v; }
HashNode* getNext() const { return next; }
void setNext(HashNode* next) { HashNode::next = next; }
void appendToChain(HashNode* last)
{
HashNode* curr = this;
while (curr->getNext() != nullptr)
{
curr = curr->getNext();
}
curr.setNext(last);
}
private:
int k;
V v;
HashNode* next;
};
Main.cpp:
#include <iostream>
#include "HashTable.h"
#include "HashNode.h"
int main()
{
std::cout << "Hello World!\n";
HashNode<int> node(1,1); //works fine
std::cout << node.getValue() << std::endl; //prints fine
HashTable<int> table(5); //throws error on compilation
}
It's probably just something stupid or that I'm blind, but here's the errors:
Error LNK1120 2 unresolved externals HashTable D:\C++\HashTable\Debug\HashTable.exe 1
Error LNK2019 unresolved external symbol "public: __thiscall HashTable<int>::HashTable<int>(int)" (??0?$HashTable#H##QAE#H#Z) referenced in function _main HashTable D:\C++\HashTable\HashTable\Main.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall HashTable<int>::~HashTable<int>(void)" (??1?$HashTable#H##QAE#XZ) referenced in function _main HashTable D:\C++\HashTable\HashTable\Main.obj 1
Also, please don't hesitate to give me pointers if my code's bad. I've never really programmed anything in C++ before so any help is welcome!
You need to move the template function definitions into the header file.
A longer answer can be found here.
Noobie programmer here learning C++ for the first time. The following is excerpted code from Teach Yourself C++ 3rd Edition.
I'm dying help me, I'm learning about classes, but I can't get this code to compile on visual studio or on Code::Blocks. :(
//#include "stdafx.h"
#include <iostream>
//I understand this. Headers, etc.
using namespace std;
//and this, name traffic management system
class myclass {
//private to myclass
int a;
public:
void set_a(int num);
int get_a();
};
/*I understand int a is private/inaccessible from the rest of the code
and void set_a(int num) is the dummy function.*/
void myclass::set_a(int num)
//not sure what this is
{
a = num;
}
/*self explanatory*/
int _tmain(int argc, _TCHAR* argv[])
{
myclass ob1, ob2;
ob1.set_a(10);
ob2.set_a(99);
cout << ob1.get_a() << "\n";
cout << ob2.get_a() << "\n";
return -5;
}
/*This is just supposed to output the number 10 and 99 right?? So why isn't it?*/
On Visual Studio the full error description is:
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall myclass::get_a(void)" (?get_a#myclass##QAEHXZ) referenced in function _wmain c:\Users\bernardo pliego\documents\visual studio 2013\Projects\Chapter 1.5\Chapter 1.5\Chapter 1.5.obj Chapter 1.5
On Code::Blocks I receive the following error:
In function 'main':
undefined reference to 'my_class::get_a()'
I am in dire need of help, can someone explain this to me?
Because you don't define get_a, you only declare it. Add a definition like this:
int myclass::get_a() { return a; }
Or just define it inline:
class myclass {
//private to myclass
int a;
public:
void set_a(int num);
int get_a() { return a };
};
You don't ever define int get_a(); so you get an error at link-time.
Include this just above your (rather hubristic) /*self explanatory*/ comment
int myclass::get_a()
{
return a;
}
I get the following error in windows.
I have a library L1 which has the following header file and it's implementation
namespace TC
{
namespace VT
{
enum Value
{
INVALID = 0,
Light = 1
};
std::ostream& operator<<(std::ostream& os, Value value);
}
}
There is another library L2 which uses the operator <<. The code compilers fine but results in a unresolved external symbol error. This means that operator << is not being located at link time.
Code in library L2.
bool Mock::Test(
TC::VT::Value venue )
{
string func(__func__);
cout << func
<< "venue = "
<< venue
<< endl;
return false;
}
cout line is the one causing the problem.
MockClient.obj : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl TC::VT::operator<<(class std::basic_ostream > &,enum TC::VT::Value)"
One of three things is happening here. Either you are not linking to the object file containing the definition of the operator, you have not provided a definition for the operator or the operator has not been defined properly. Since you have stated the object file is being linked you should make sure that your definition of the operator is correct. Your definition should look like the following
std::ostream& TC::VT::operator<<(std::ostream& os, Value value)
{
// ... your code here
return os;
}
Or it should be wrapped in the namespace
namespace TC
{
namespace VT
{
std::ostream& TC::VT::operator<<(std::ostream& os, Value value)
{
// ... your code here
return os;
}
}
}
I'm trying to write the global function:
std::ostream& operator<<(std::ostream& out, const Str& str)
{
for(int i = 0; i < (int)str.mSize; ++i)
out << str.mBuffer[i];
return out;
}
For a custom string class. It compiles fine but when I go to link:
1>Str.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Str const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABVStr###Z) already defined in Main.obj
1>C:\Users\Ron\Documents\Visual Studio 2010\Projects\App\Debug\App.exe : fatal error LNK1169: one or more multiply defined symbols found
How could multiple definitions for this exist? I just created the class Str.
I think you have defined twice in Main.cpp and Str.cpp, or may be .h files.
Write a str.h files which include the declaration of the Str class:
//str.h
class Str {
// your class stuff
friend std::ostream& operator<<(std::ostream& out, const Str& str);
};
then in str.cpp:
//str.cpp
#include "str.h"
std::ostream& operator<<(std::ostream& out, const Str& str) {
for(int i = 0; i < (int)str.mSize; ++i)
out << str.mBuffer[i];
return out;
}
Then you can use the function in you main.cpp.
If you define a function in a header file and include it twice, you'll get a multiple definition error, which you have.
To remedy this, declare the function in the header with a prototype and define it in a .cpp file.
Alternately, if you're trying to make a header only library, you could do
class Str {
// your class stuff
friend std::ostream& operator<<(std::ostream& out, const Str& str) {
for(int i = 0; i < (int)str.mSize; ++i)
out << str.mBuffer[i];
return out;
}
};
Did you put this in a header file?
The right way to do it is to declare it in the header file and put the code in a source file.