我想要一些数据缓存,其中包含一些我可以通过UpdateCache函数更新的对象.但是,我遇到的问题是a LNK2001后跟a LNK1120.
HeaderFile.h
#ifndef headerfile_included
#define headerfile_included
#include <vector>
struct MyObject {
unsigned int A;
unsigned int B;
};
class MyClass {
private:
static std::vector<MyObject> myObjectCache;
public:
static void UpdateCache ();
};
#endif
Run Code Online (Sandbox Code Playgroud)
CodeFile.cpp
#include "HeaderFile.h"
void MyClass::UpdateCache () {
myObjectCache.clear();
/* Repopulate cache with new data */
}
Run Code Online (Sandbox Code Playgroud)
我从链接获得的错误消息是
错误LNK2001:未解析的外部符号""private:static class std :: vector> MyClass :: myObjectCache"(?myObjectCache @ MyClass @@ 0V?$ vector @ UMyObject @@ V?$ allocator @ UMyObject @@@ std @@@ STD @@ …
我不知道如何解决这些错误.我写了整个代码,并试图看看它是否是括号的问题,但也没有帮助.我不知道它是什么线但我相信它是在这个功能.
错误LNK2019:未解析的外部符号"int __cdecl read_file_in_array(int(*const)[3])"(?read_file_in_array @@ YAHQAY02H @ Z)在函数_main致命错误中引用LNK1120:1未解析的外部
我相信这个与括号有关
#include <iostream>
#include <fstream>
using namespace std;
int read_file_in_array(int exam[100][3]);
double calculate_total(int exam1[], int exam2[], int exam3[]); // function that calcualates grades to see how many 90,80,70,60
//void display_totals();
double exam[100][3];
int read_file_in_array(double exam[100][3])
{
ifstream infile;
int exam1[100];
int exam2[100];
int exam3[100];
infile.open("grades.txt");// file containing numbers in 3 columns
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
}
int num, i=0,j=0;
while(!infile.eof()) // reads …Run Code Online (Sandbox Code Playgroud) PacketBuilder是一个允许写入char*数组的类.附加功能:
template <class T>
void PacketBuilder::Append(const T value)
{
memcpy((&m_Buffer) + m_Index, (const void*) &value, sizeof(T));
m_Index += sizeof(T);
}
Run Code Online (Sandbox Code Playgroud)
编译没有错误.如果我调用Append并使用T作为无符号短(WORD).它很棒.如果我使用T作为unsigned char.我收到链接器错误.
m_Builder.Append<unsigned char>(0x01); // Error: LNK1120
m_Builder.Append<unsigned short>(0x0001); // Works
Run Code Online (Sandbox Code Playgroud)
来自VS2010的错误(我得到了德国vs2010):
错误LNK2019:Verweis aufnichtaufgelöstesexternes符号""public:void __thiscall PacketBuilder :: Append(unsigned char)"(?? $ Append @ E @ PacketBuilder @@ QAEXE @ Z)"在Funktion中""public:void __thiscall客户端: :DoHandshake(无效)"(?DoHandshake @ Client @@ QAEXXZ)".1> C:\ XXX\C++\SilkroadEmu\Debug\LoginServer.exe:致命错误LNK1120:1nichtaufgelösteexterneVerweise.
翻译成英文:
错误LNK2019:未解析的外部符号""public:void __thiscall PacketBuilder :: Append(unsigned char)"(?? $ Append @ E @ PacketBuilder @@ QAEXE @ Z)"in Function""public:void __thiscall Client :: DoHandshake(无效)"(?DoHandshake @ …
我只是在学习C++中的模板编程,并且链接器无法找到我的class'es构造函数的定义.可能是什么原因?代码如下.
Logger.h
template <class T>
class Logger {
public:
Logger(NodeHandle& nh, char* topic, short pubFrequency);
virtual ~Logger();
void publish();
T& getMsg();
private:
NodeHandle& nh_;
Publisher publisher_;
T msg_;
const char* topic_;
const short pubFrequency_;
};
Run Code Online (Sandbox Code Playgroud)
Logger.cpp
template <class T>
Logger<T>::Logger(NodeHandle& nh, char* topic, short pubFrequency) :
nh_(nh),
topic_(topic),
pubFrequency_(pubFrequency),
publisher_(topic_, static_cast<Msg*>(&msg_)) {}
template <class T>
Logger<T>::~Logger() {}
Run Code Online (Sandbox Code Playgroud)
然后,当我尝试在main.cpp中创建一个Logger实例时
NodeHandle nh;
Logger<std_msgs::String> logger(nh, "test", 10);
Run Code Online (Sandbox Code Playgroud)
链接器抱怨:
undefined reference to `Logger<std_msgs::String>::Logger(NodeHandle&, char*, short)'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?没有编译器错误,所以所有包含都在那里,我猜.
好吧,我正在为我的类编写这个代码,并且我使用Visual Studio获得了链接器错误LNK2019和LNK1120.我不太清楚为什么它正在做它正在做的事情,但我离题了.
头文件:
#ifndef PointClass
#define PointClass
#include <iostream>
namespace PointClass
{
class point
{
public:
// CONSTRUCTOR
point(double initial_x = 0.0, double initial_y = 0.0);
// MODIFICATION MEMBER FUNCTIONS
void shift(double x_amount, double y_amount);
void rotate90( );
// CONSTANT MEMBER FUNCTIONS
double get_x( ) const { return x; }
double get_y( ) const { return y; }
// FRIEND FUNCTION
friend std::istream& operator >>(std::istream& ins, point& target);
double x, y; // x and y coordinates of this point
}; …Run Code Online (Sandbox Code Playgroud) 当我尝试调用OnLoop时,我收到错误,它无法识别它.
///Ins_App.h
#ifndef INS_APP_H
#define INS_APP_H
#include <SDL/SDL.h>
class Ins_App
{
private:
/* Variables */
bool Running;
SDL_Surface* Surf_Display;
public:
/* inMain */
Ins_App();
int OnExecute();
public:
/* Other */
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
protected:
};
#endif // INS_APP_H
Run Code Online (Sandbox Code Playgroud)
///Ins_App.cpp
#include "Ins_App.h"
Ins_App::Ins_App()
{
Running = true;
Surf_Display = NULL;
}
int Ins_App::OnExecute(){
if(OnInit() == false){
return -1;
}
SDL_Event Event;
while(Running){
while(SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
return 0;
}
int main(int …Run Code Online (Sandbox Code Playgroud) 它编译,但它不会从.o文件链接到可执行文件.我已经取出了我现在和它运作良好的一切(基本上,str_to_int中的所有内容).
我的代码:
#include <stdio.h>
#include <stdlib.h>
int str_to_int( char string[] ) {
// takes null-terminate ascii string, returns int value
// value of digit at i = (value*10)+(str[i]-'0').
int num=0;
int sign=0;
return num;
}
int main( int argc, char* argv[] ) {
if (argc==1) {
printf( "usage: review_grades score1 [score2 ...]\n" );
printf( "note: score values are non-negative.\n" );
}
else {
printf( "population: %u \n", argc-1 ); // population
printf( "%u \n", str_to_int(argv[1]));
}
return EXIT_SUCCESS ;
} …Run Code Online (Sandbox Code Playgroud) 我该如何解决此错误:
Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
Run Code Online (Sandbox Code Playgroud)
我还定义了cai.cpp中的所有函数.但是由于代码行太多,我没有上传它.我确实将.h文件包含在cai.cpp中.我对这个错误的问题感到困惑.
main.cpp文件
#include "cai.h"
int main()
{
CAI test;
test.StartTest();
}
Run Code Online (Sandbox Code Playgroud)
cai.h文件
class CAI
{
public:
void StartTest();
bool AskRandomMultiplicationQuestion();
bool AskRandomDivisioQuestion();
private:
void PrintRandomGoodJob();
void PrintRandomEncouragementMessage();
int ChooseRandomNumber();
void PrintTestSummary(int, int, int);
};
Run Code Online (Sandbox Code Playgroud) 部分源代码:
double _Complex z = 1.0 + 1.0*I;
printf("%f\n", cabs(z));
Run Code Online (Sandbox Code Playgroud)
我的开发环境:Ubuntu16.04LTS,Clion IDE,GCC版本5.4.0,C11标准.
当我运行代码时,消息发生错误
undefined reference to `cabs'
Run Code Online (Sandbox Code Playgroud)
IDE告诉我该函数cabs是在头文件中声明的cmathcalls.h,所以我尝试:
#include<cmathcalls.h>
Run Code Online (Sandbox Code Playgroud)
但IDE警告我无法找到该文件,所以我再次尝试:
#include<bits/cmathcalls>
Run Code Online (Sandbox Code Playgroud)
我运行代码,但它仍然无法运行.
我想知道如何通过z函数得到复数的abs值cabs?
根据一个定义规则,类和结构定义不得在单个翻译单元中重复。
但是,为什么这样的话,如果它们实际上是定义,则允许以多个翻译单元重复它们。在这种情况下,链接器为什么不引发多定义错误?
例如-以下操作应通过相同的逻辑引发多个定义错误
测试
#ifndef TEST
#define TEST
class s {
int a;
int b;
};
#endif
Run Code Online (Sandbox Code Playgroud)
test1.cpp
#include "test.h"
int main() {}
Run Code Online (Sandbox Code Playgroud)
test2.cpp
#include "test.h"
Run Code Online (Sandbox Code Playgroud)