c++ error: referenced in function _main when reading a file - c++

When I'm trying to read a file with a function
void readFile(string fileName, string* anArray) {
unsigned int lineCounter = 0;
ifstream inFile = ifstream(fileName);
while (!inFile.eof()) {
string fileLine;
inFile >> fileLine;
if (!fileLine.empty()) {
anArray[lineCounter] = fileLine;
++lineCounter;
}
}
inFile.close();
}
I get the error below, which I assume is because of the pointer on the string array ?
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl
resource::readFile(class std::basic_string,class std::allocator >,class
std::basic_string,class
std::allocator > *)"
(?readFile#resource##YAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAV23##Z)
referenced in function _main

void readFile(string fileName, string* anArray) {
This is the definition of a member function, but you forgot to write the class name.
void resource::readFile(string fileName, string* anArray) {
As you have it now, you've defined a new function in the global namespace that has nothing to do with resource, so when main tries to use resource::readFile, the definition cannot be found.

Related

linker errors. no idea what these errors means [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 5 years ago.
i'm just writing an array class as practice in Microsoft Visual Studio 2010 but i'm getting some annoying errors. here they are:
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::~arrays<int>(void)" (??1?$arrays#H##QAE#XZ) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: int & __thiscall arrays<int>::operator[](int)" (??A?$arrays#H##QAEAAHH#Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: bool __thiscall arrays<int>::operator==(class arrays<int> const &)const " (??8?$arrays#H##QBE_NABV0##Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: int const & __thiscall arrays<int>::operator=(class arrays<int> const &)" (??4?$arrays#H##QAEABHABV0##Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::arrays<int>(class arrays<int> const &)" (??0?$arrays#H##QAE#ABV0##Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class arrays<int> const &)" (??5#YAAAV?$basic_istream#DU?$char_traits#D#std###std##AAV01#ABV?$arrays#H###Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class arrays<int> const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABV?$arrays#H###Z) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: int __thiscall arrays<int>::getsize(void)const " (?getsize#?$arrays#H##QBEHXZ) referenced in function _wmain
1>test.obj : error LNK2019: unresolved external symbol "public: __thiscall arrays<int>::arrays<int>(int)" (??0?$arrays#H##QAE#H#Z) referenced in function _wmain
1>c:\users\bm\documents\visual studio 2010\Projects\arrays\Debug\arrays.exe : fatal error LNK1120: 9 unresolved externals
how can I fix them?
here is my code:
arrays.h
#ifndef ARRAYS_H
#define ARRAYS_H
#include <iostream>
using namespace std;
template <typename T>
class arrays{
friend ostream &operator<<(ostream &output, const arrays &a);
friend istream &operator>>(istream &input, const arrays &a);
public:
arrays(int = 10);
arrays(const arrays &);
~arrays();
int getsize() const;
const T &operator=(const arrays &);
bool operator==(const arrays &) const;
bool operator!=(const arrays &right) const{
return !((*this)==right);
}
T &operator[](int);
T operator[](int) const;
private:
int size;
T *ptr;
};
#endif
arrays.cpp
#include "stdafx.h"
#include <iostream>
#include "arrays.h"
#include <cstdlib>
using namespace std;
template <typename T>
arrays<T>::arrays(int mysize){
size = mysize;
ptr = new(int[size]);
for(int i = 0; i< size; i++)
ptr[i] = 0;
}
template <typename T>
arrays<T>::arrays(const arrays<T> &myarray){
size = myarray.size;
ptr = new(int[size]);
for(int i = 0; i< size; i++){
ptr[i] = myarray.ptr[i];
}
}
template <typename T>
arrays<T>::~arrays(){
delete [] ptr;
}
template <typename T>
int arrays<T>::getsize() const {
return size;
}
template <typename T>
const T &arrays<T>::operator=(const arrays<T> &right){
if ( &right != this){
if(size != right.size){
delete [] ptr;
size= right.size;
ptr = new(int[size]);
}
for(int i =0; i < size; i++)
ptr[i] = right.ptr[i];
}
return *this;
}
template <typename T>
bool arrays<T>::operator==(const arrays<T> &right) const{
if(right.size != size)
return false;
for(int i = 0; i<size; i++)
if(ptr[i] != right.ptr[i])
return false;
return true;
}
template <typename T>
T &arrays<T>::operator[](int subscript) {
if(subscript < 0 || subscript >= size){
cout << "error: subscript out of range";
}
return ptr[subscript];
}
template <typename T>
T arrays<T>::operator[](int subscript) const {
if(subscript < 0 || subscript >= size){
cout << "error: subscript out of range";
exit(1);
}
return ptr[subscript];
}
template <typename T>
istream &operator>>(istream &input, const arrays<T> &a){
for(int i = 0; i< a.size; i++)
input >> a.ptr[i];
return input;
}
template <typename T>
ostream &operator<<(ostream &output, arrays<T> &a){
for(int i= 0; i<a.size;i++)
output << a.ptr[i];
return output;
}
any help would be appreciated.
Implementation code for a template class must live in the header, not in the .cpp file. This is required so that other code that use your template class can "instanciate" it's code based on the template parameter.
So just move all your code from your .cpp to your .h and you're good to go.

Loki's SmallObjAllocator of of memory

#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),0){};
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier.Deallocate(myChar, 1);
return 0;
}
The loki library uses essentially generic programming in c++
I have got the code up there using Small object allocator of memory(Loki::SmallObjAllocator)
I m using visual c++ 2010
I get those errors:
> MyLoki.cpp
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void __thiscall Loki::SmallObjAllocator::Deallocate(void *,unsigned int)" (?Deallocate#SmallObjAllocator#Loki##QAEXPAXI#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "public: void * __thiscall Loki::SmallObjAllocator::Allocate(unsigned int,bool)" (?Allocate#SmallObjAllocator#Loki##QAEPAXI_N#Z) referenced in function _wmain
1>MyLoki.obj : error LNK2019: unresolved external symbol "protected: __thiscall Loki::SmallObjAllocator::SmallObjAllocator(unsigned int,unsigned int,unsigned int)" (??0SmallObjAllocator#Loki##IAE#III#Z) referenced in function "public: __thiscall MySmallObj::MySmallObj(void)" (??0MySmallObj##QAE#XZ)
I have founded an answer to my first question.
#include "stdafx.h" //In order to use Visual C++
#include <iostream>
#include <Loki\SmallObj.h> //The header file to manage
// smalls objects allocator
class MySmallObj : public Loki::SmallObjAllocator //inherit from the base
//class SmallObjAllocator
{
public:
MySmallObj():SmallObjAllocator(sizeof(char), sizeof(long),1){}; //the chunkSize < maxObjsize
};
int _tmain(int argc, _TCHAR* argv[])
{
MySmallObj * premier = new MySmallObj; //declaring my object derived from smallobjallcator
char * myChar = static_cast<char*>( premier->Allocate(1, true)); //calling allocate from my object and conveting the void pointer to char*
premier->Deallocate(myChar, 1);
return 0;
}
the errors of multiple
unresolved external symbol
are because of SmallObj.cpp , of Loki that was not included to the project
for example of Loki::SmallObjAllocator, Loki::SmallObject here

C++ Unresolved symbol

I just don't see where I went wrong.
The compiler complains about
Error 215 error LNK2001: Unresolved external symbol ""class std::vector<class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >,class std::allocator<class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > > > __cdecl splitW(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" (?splitW##YA?AV?$vector#V?$basic_string#_WU?$char_traits#_W#std##V?$allocator#_W#2##std##V?$allocator#V?$basic_string#_WU?$char_traits#_W#std##V?$allocator#_W#2##std###2##std##AAV?$basic_string#_WU?$char_traits#_W#std##V?$allocator#_W#2##2#ABV32##Z)". C:\voice\clsText.obj voice
But I don't see where I went wrong. Can somebody tell me how to analyze this error message? I get it quite often, but then again I never remember how I solved it.
Thank you very much!
This is my code in strhelper.cpp:
vector<wstring> splitW(const wstring& uMain, const wstring &uSplitBy)
{
vector<wstring>s;
int iStart=0;
for (;;)
{
int iPos=uMain.find(uSplitBy,iStart);
if (iPos==-1)
{
wstring s1;
s1 = uMain.substr(iStart,uMain.size() - iStart);
if (s1.size()>0)
{
s.push_back(s1);
}
break;
}
else
{
wstring s2;
s2 = uMain.substr(iStart,iPos-iStart);
s.push_back(s2);
iStart = iPos + 1;
}
}
return s;
}
And this is the part of the header that contains the declaration:
void replaceOnce(wstring& uText,const wstring& uSearchFor,const wstring& uReplaceWith,bool uTextCompare);
vector<wstring> splitW(wstring &str, const wstring &uSep);
vector<wstring> splitAToWVec(const string& uMain, const string& uSplitBy);
vector<string> splitAToAVec(const string& uMain, const string& uSplitBy);
The type of the 1st parameter of splitW() is declared as wstring& in header file but defined as const wstring& in source code. Please make them consistent.
Declaration:
vector<wstring> splitW( wstring& str, const wstring &uSep);
Definition:
vector<wstring> splitW(const wstring& uMain, const wstring &uSplitBy)

Object already defined but I'm not sure where

I'm new to C++ and the error codes aren't really compelling to me.
I am trying to build a simple C++ OOP program as a homework assignment but which will gather then display information about book etc.
I am getting an error:
1>Book.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Author::getFullName(void)" (?getFullName#Author##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) already defined in Author.obj
1>Book.obj : error LNK2005: "public: void __thiscall Author::setFirstName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setFirstName#Author##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Author.obj
1>Book.obj : error LNK2005: "public: void __thiscall Author::setLastName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setLastName#Author##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Author.obj
1>Publisher.obj : error LNK2005: "public: __thiscall Publisher::Publisher(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Publisher##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##00#Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: __thiscall Publisher::Publisher(void)" (??0Publisher##QAE#XZ) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Publisher::getPublisherInfo(void)" (?getPublisherInfo#Publisher##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: void __thiscall Publisher::setAddress(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setAddress#Publisher##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: void __thiscall Publisher::setCity(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setCity#Publisher##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Book.obj
1>Publisher.obj : error LNK2005: "public: void __thiscall Publisher::setName(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setName#Publisher##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Book.obj
1>C:\Users\pc\Desktop\School\ITS 340\Labs\Lab 1\Lab 1\Debug\Lab 1.exe : fatal error LNK1169: one or more multiply defined symbols found
Am I missing something here?
Book.cpp
#include <iostream>
using namespace std;
#include "Author.cpp"
#include "Publisher.cpp"
class Book
{
public:
Book();
Book(string title, Author *pAuthor, Publisher *pPublisher, double price);
~Book();
void setTitle(string title);
void setAuthorName(string first, string last);
void setPublisher(string name, string address, string city);
void setPrice(double price);
string convertDoubleToString(double number);
string getBookInfo();
private:
string title;
double price;
Author *pAuthor;
Publisher *pPublisher;
};
Book::Book()
{
}
Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
title = title;
price = price;
}
Book::~Book()
{
}
void Book::setTitle(string title)
{
}
void Book::setAuthorName(string first, string last)
{
}
void Book::setPublisher(string name, string address, string city)
{
}
void Book::setPrice(double price)
{
}
string Book::convertDoubleToString(double number)
{
return 0;
}
string Book::getBookInfo()
{
return 0;
}
Publisher.cpp
#include <iostream>
using namespace std;
class Publisher
{
public:
Publisher();
Publisher(string name, string address, string city);
string getPublisherInfo();
void setName(string name);
void setAddress(string address);
void setCity(string city);
private:
string name;
string address;
string city;
};
Publisher::Publisher()
{
}
Publisher::Publisher(string name, string address, string city)
{
name = name;
address = address;
city = city;
}
string Publisher::getPublisherInfo()
{
return "0";
}
void Publisher::setName(string name)
{
name = name;
}
void Publisher::setAddress(string address)
{
address = address;
}
void Publisher::setCity(string city)
{
city = city;
}
You should never include a cpp file in another cpp file or in a header. This almost inevitably leads to duplicate object definitions.
Put declarations into the header file, and definitions into the cpp files. Include headers in cpp files where you need to have access to methods of objects defined in other cpp files.
Take Publisher.cpp as an example: the top part up to and including the }; line need to go into the Publisher.h file. Everything else has to remain in the Publisher.cpp. In addition, you need to add #include Publisher.h to the top of Publisher.cpp, along with other headers that you need to include.
You also need to remove using namespace std; from the header, and use std::string in your declarations. It is OK to put using in your cpp file, though.
Use #IFNDEF to avoid duplicate object definitions if you are really in need to add some cpp to another cpp.
Like
#include <iostream>
using namespace std;
#ifndef CPPADDED
#define CPPADDED
class Publisher
{...}
#endif
In addition to what Dasblinkenlight said,
Your problem is that you did not use ifndef guard. You should use that to avoid this error. A quick search will give you the hint how to add headers.
Create header file example: resource.h and put there prototypes of functions also class.
So it will look like:
#include <iostream>
#include <string>
using namespace std;
class Book
{
public:
Book();
Book(string title, Author *pAuthor, Publisher *pPublisher, double price);
~Book();
void setTitle(string title);
void setAuthorName(string first, string last);
void setPublisher(string name, string address, string city);
void setPrice(double price);
string convertDoubleToString(double number);
string getBookInfo();
private:
string title;
double price;
Author *pAuthor;
Publisher *pPublisher;
};
class Publisher
{
public:
Publisher();
Publisher(string name, string address, string city);
string getPublisherInfo();
void setName(string name);
void setAddress(string address);
void setCity(string city);
private:
string name;
string address;
string city;
};
class Author{
//... prototypes
};
Edit Book.cpp to this:
#include "resource.h"
Book::Book()
{
}
Book::Book(string title, Author *pAuthor, Publisher *pPublisher, double price)
{
title = title;
price = price;
}
Book::~Book()
{
}
void Book::setTitle(string title)
{
}
void Book::setAuthorName(string first, string last)
{
}
void Book::setPublisher(string name, string address, string city)
{
}
void Book::setPrice(double price)
{
}
string Book::convertDoubleToString(double number)
{
return 0;
}
string Book::getBookInfo()
{
return 0;
}
And Publisher.cpp to this:
#include "resource.h"
Publisher::Publisher()
{
}
Publisher::Publisher(string name, string address, string city)
{
name = name;
address = address;
city = city;
}
string Publisher::getPublisherInfo()
{
return "0";
}
void Publisher::setName(string name)
{
name = name;
}
void Publisher::setAddress(string address)
{
address = address;
}
void Publisher::setCity(string city)
{
city = city;
}
And try to do same with Author.cpp,so you put function's prototypes (class) to "resource.h" and include "resource.h" to Author.cpp.

leveldb example not working on windows : Error LNK2029

In order to test leveldb, I tried to reproduce the leveldb's example on VS 2008.
#include <assert.h>
#include "leveldb/db.h"
int main()
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options,"D:\dev\tools\tmp",&db);
}
I have included leveldb/include directory and linked libleveldb.lib.
Result :
error LNK2019: unresolved external symbol "public: static class
leveldb::Status __cdecl leveldb::DB::Open(struct leveldb::Options
const &,class std::basic_string,class std::allocator > const &,class
leveldb::DB * *)"
(?Open#DB#leveldb##SA?AVStatus#2#ABUOptions#2#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##PAPAV12##Z)
referenced in function _main
error LNK2019: unresolved external symbol public: __thiscall leveldb::Options::Options(void)"
(??0Options#leveldb##QAE#XZ) referenced in function _main
Does anyone know how to fix this ?
Solution :
Use levelDb-portable from zhangyafreikimi