Visual Studio C++ 2010 in-project link error - c++

I have a multi-file project in VSC++2010, but for some reason it won't link some of them properly.
For example, I have CParser.h and CParser.cpp . CParser.h is just some function declarations:
#pragma once
#include <string>
void parseArg(int argc, char* argv[], GVar gv);
void parseCfg(string cfg, GVar gv)
CParser.cpp just contains implementations:
#include <cstdio>
#include <fstream>
#include <cstring>
#include <string>
#include "_GlobalVar.h" //defines GVar, not relevant
#include "CParser.h"
void parseArg(int argc, char* argv[], GVar &gv) {
/*not really relevant*/
}
And the error:
main.obj : error LNK2019: unresolved external symbol "void __cdecl
parseArg(int,char * * const,class GVar)"
(?parseArg##YAXHQAPADVGVar###Z) referenced in function _SDL_main
Edit:
There's also this other problem:
template<class T>
void RDAMHandler<T>::clean() {
long i;
while(!avtick.empty())
avtick.pop();
for(i = v.size() - 1; i >= 0; i--) {
delete all[i];
all.pop_back();
v.pop_back();
}
}
And the declaration:
template<class T>
class RDAMHandler {
vector<T*> all;
priority_queue<long> avtick;
vector<bool> v;
public:
T &operator[](long x);
long insert(T &x);
void del(long x);
void clean();
};
I don't see any difference here; what is the problem?
Edit edit: And error
main.obj : error LNK2019: unresolved external symbol "public: void
__thiscall RDAMHandler::clean(void)" (?clean#?$RDAMHandler#USDL_Surface####QAEXXZ) referenced in function
"void __cdecl cleanUp(class GVar)" (?cleanUp##YAXVGVar###Z)

In CParser.cpp
I think You have to use statement
void CParser::parseArg(int argc, char* argv[], GVar &gv)
instead of
void parseArg(int argc, char* argv[], GVar &gv) in CParser.cpp file
And In CParser.h
The declaration should be changed to void parseArg(int argc, char* argv[], GVar &gv);
And For Next Error
For Reference Please Go through this
1. Template using class
Hope this will help you.

They're two different overloads - the declaration in the header has GVar gv, while the definition in the .cpp file has GVar &gv. One of these is probably a typo.

Related

Unresolved external symbol error using uint16_t c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
endianness.cpp
#include "endianness.h"
#include <cstdint>
using namespace io;
void switch_endianness(uint16_t* n)
{
*n = (*n >> 8) | (*n << 8);
}
void switch_endianness(uint32_t* n)
{
...
}
void switch_endianness(uint64_t* n)
{
...
}
endianness.h
#ifndef ENDIANNESS_H
#define ENDIANNESS_H
#include <cstdint>
namespace io
{
void switch_endianness(uint16_t* n);
void switch_endianness(uint32_t* n);
void switch_endianness(uint64_t* n);
}
#endif
Trying to test out my switch_endianness function in app.cpp, I get an unresolved symbol error:
LNK2019 unresolved external symbol "void __cdecl io::switch_endianness(unsigned short *)" (?switch_endianness#io##YAXPEAG#Z) referenced in function main
app.cpp
#ifndef TEST_BUILD
#include <iostream>
#include "io/endianness.h"
int main(int argn, char** argv)
{
std::uint16_t y = 0x0000;
io::switch_endianness(&y);
std::cout << y;
}
#endif
How I understand and read thus far, its a linking/reference problem. I think my code is fine and should compile and run as intended, I did 'include in project' to my endianness files, perhaps there is something trivial I'm missing or doing wrong with referencing? I really can't seem to solve this.
In my endianness.cpp file I changed the function declarations to include the folder directory it was in and now it works.. (As it was complaining about this in the header file not being able to find function definition)
void io::switch_endianness(uint16_t* n)
{
...
}

Linker issue in Visual Studio C++

I'm trying to get started with C++ in VS 2017 (empty project template), but immediately ran into linker problems when adding 1 simple class, so I guess I'm missing something important...
My project looks like this:
test.h:
#include <iostream>
class test
{
public:
test();
~test();
std::string getInfo();
};
test.cpp:
#include "test.h"
test::test() {}
test::~test() {}
std::string getInfo() {
return "test";
}
And main.cpp:
#include <iostream>
#include <string>
#include "test.h"
int main(int argc, char **argv) {
test t;
std::cout << "output: " << t.getInfo() << std::endl;
return 0;
}
The linker error I get is the infamous LNK2019:
LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl test::getInfo(void)" (?getInfo#test##QEAA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) referenced in function main
Any ideas what I am doing wrong here?
Thanks!
In file test.cpp you need to properly specify the scope of the member function:
std::string test::getInfo() {
return "test";
}
Note the test:: before the getInfo()

Static Lib using namespace leads to unresolved external

I am using VS2013 and I have a static lib project with the following header:
#pragma once
namespace StaticLibNamespace
{
void foo( void );
}
Then the function is defined in the cpp as follows:
#include "stdafx.h"
#include "StaticLibHeader.h"
using namespace StaticLibNamespace;
void foo( void )
{
;
}
In my simple console app, I include the reference to StaticLibNameSpaceTest.lib and my main function is the following:
#include "stdafx.h"
#include "..\StaticLibNamespaceTest\StaticLibHeader.h"
int _tmain(int argc, _TCHAR* argv[])
{
StaticLibNamespace::foo();
return 0;
}
If I try and compile this I get the following error:
NamespaceTest.obj : error LNK2019: unresolved external symbol "void __cdecl StaticLibNamespace::foo(void)" (?foo#StaticLibNamespace##YAXXZ) referenced in function _wmain
However if I change my static lib cpp file to the following everything is fine:
#include "stdafx.h"
#include "StaticLibHeader.h"
void StaticLibNamespace::foo( void )
{
;
}
I'm obviously not understanding everything going on with "using namespace" can someone please enlighten me? Thanks!
The using namespace directive changes the lookup rules for symbols when the compiler sees an unqualified name and needs to find what it refers to.
However, in your case, you are defining a new function called foo. As an unqualified name, this defines a new foo in the global namespace (assuming there wasn't already one there). When you qualify the name, you are defining StaticLibNamespace::foo as you intend.
A different solution might be:
namespace StaticLibNamespace {
void foo( void )
{
;
}
} // namespace StaticLibNamespace

Problems with headers and linker errors (new to C++)

I'm new to c++ and I'm having some trouble with making a header file. The exact error I'm getting is
obj.obj : error LNK2019: unresolved external symbol "float * __cdecl getVertices(class std::basic_string,class std::allocator >,int,float *)" (?getVertices##YAPAMV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##HPAM#Z) referenced in function "struct ObjModel __cdecl importObj(void)" (?importObj##YA?AUObjModel##XZ)
The bugs/solutions I'm seeing seem much more complicated that what I'm doing. Here's my header, which I suspect is wrong.
//obj.h
#ifndef OBJ_H_INCLUDED
#define OBJ_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
struct ObjVertex {
float x, y, z;
};
struct ObjTriangle {
int Vertex[3];
int Normal[3];
};
struct ObjModel {
int NumVertex, NumNormal, NumTexCoord, NumTriangle;
ObjVertex *VertexArray;
ObjVertex *NormalArray;
ObjTriangle *TriangleArray;
};
//function prototypes
float* getVertices(string buf, int i, float* ret);
ObjModel importObj();
char* subString(char* buf, int b, int e);
#endif
I've just started in C++ but I have experience in Java and C, so it's probably an issue with me not knowing some C++ specific thing.
There is no implementation for float* getVertices(string buf, int i, float* ret); hence you get a linker error.
You must attach module where getVertices is declared to the project.

unresolved external errors

I have the following .h and .cpp files
If i have to I will include the full codes of the function definitions
When i compile my program i get the errors shown at the end
hash.h
#define BUCKETS 64
#define B_ENTRIES 50000
int curr_tanker;
typedef unsigned long int ulong;
typedef struct bucket
{
int bucket_id;
ulong bucket_entries;
}bucket;
typedef struct tanker_record
{
ulong tanker_id;
ulong tanker_size;
ulong num_of_entries;
ulong bucket_entry_count;
}tanker_record;
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
struct fpinfo* InitHTable(fpinfo *);
int CreateTanker(tanker_record tr[]);
int Hash_CreateEntry(struct fpinfo *,struct fpinfo he,tanker_record tr);
ht.cpp
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <iostream>
#include "ht.h"
struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
}
int CreateTanker(tanker_record tr[])
{
}
int
Hash_CreateEntry(struct fpinfo *t[][B_ENTRIES],struct fpinfo he,tanker_record tr[])
{
}
static void
WriteHTtoFile(struct fpinfo *t[][B_ENTRIES],int this_tanker)
{
}
main.cpp
#include<iostream>
#include"ht.cpp"
#include<conio.h>
#include<stdlib.h>
void main(int argc, char **argv)
{
static fpinfo hash_table[BUCKETS][B_ENTRIES];
static tanker_record tr[100];
InitHTable(&hash_table[0][0]);
CreateTanker(tr);
struct fpinfo fp;
...
ar = Hash_CreateEntry(&hash_table[0][0], fp,tr[0]);
i get the following errors when i try to compile it using vc2010
1>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable##YAPAUfpinfo##QAY0MDFA#U1##Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)"
(?CreateTanker##YAHQAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl Hash_CreateEntry(struct fpinfo * (* const)[50000],struct fpinfo,struct tanker_record * const)" (?Hash_CreateEntry##YAHQAY0MDFA#PAUfpinfo##U1#QAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker##3HA) already defined in ht.obj
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)"
(?Hash_CreateEntry##YAHPAUfpinfo##U1#Utanker_record###Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable##YAPAUfpinfo##PAU1##Z) referenced in function _main
THANKS FOR YOUR HELP!!
You're including ht.cpp from main.cpp, which will include all the definitions of functions already defined in ht.cpp itself.
You want to include ht.h instead.
It won't help in this situation, but you should also protect the header file with include guards:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
You also need the arguments of the function declarations to match those of the definitions:
struct fpinfo* InitHTable(struct fpinfo[][B_ENTRIES]);
// Missing: ^^^^^^^^^^^
int CreateTanker(tanker_record tr[]); // OK
int Hash_CreateEntry(struct fpinfo*[][B_ENTRIES],struct fpinfo,tanker_record[]);
// Missing ^^^^^^^^^^^^^ ^^
Add an "include guard" in your header, so that it its contents aren't "seen" twice after preprocessing. For Microsoft, #pragma once at the beginning of the .h file. In general, add:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
Make sure to adopt a consistent "unique" naming scheme for each of your headers. __YOUR_HEADER_H would do, for example customio.h into __CUSTOM_IO_H.