简体   繁体   English

我在做什么错?,用C ++链接

[英]What am I doing wrong?, linking in C++

I'm trying to code a simple base64 encoder/decoder (to test my programming skill). 我正在尝试编写一个简单的base64编码器/解码器(以测试我的编程技能)。

I can compile it, but it doesn't link, I've this message error: 我可以编译它,但是它不链接,我收到此消息错误:

C:\\Documents and Settings\\Facon\\Escritorio>g++ base64.o main.o -o prueba.exe C:\\ Documents and Settings \\ Facon \\ Escritorio> g ++ base64.o main.o -o prueba.exe

main.o:main.cpp:(.text+0x24a): undefined reference to `Base64Encode(std::vector > const&)' main.o:main.cpp :(。text + 0x24a):对Base64Encode(std :: vector> const&)的未定义引用

collect2: ld returned 1 exit status collect2:ld返回1退出状态

Compiler & Linker: Mingw32 3.4.5 SO: Windows XP 编译器和链接器:Mingw32 3.4.5 SO:Windows XP

This is my source code: 这是我的源代码:

base64.h: base64.h:

#ifndef BASE64_H
#define BASE64_H

#include <iostream>
#include <vector>

typedef unsigned char byte;

std::string Base64Encode(const std::vector<byte> &array);
std::vector<byte> Base64Decode(const std::string &array);

#endif

base64.cpp: base64.cpp:

#include "base64.h"

std::string Base64Encode(std::vector<byte> &array)
{
 const char *base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 const unsigned int size = array.size();
 std::string output;

 for (unsigned int i = 0; (i < size); i++)
 {
  if ((size - i) > 3)
  { 
   output.push_back(static_cast<char>(base64_table[array[i] >> 2]));
   output.push_back(static_cast<char>(base64_table[((array[i++] & 0x03) << 4) | ((array[i] & 0xF0) >> 4)]));
   output.push_back(static_cast<char>(base64_table[((array[i++] & 0x0F) << 2) | ((array[i] & 0xC0) >> 4)]));
   output.push_back(static_cast<char>(base64_table[array[i] & 0x3F]));
  }
  else if ((size - i) == 3)
  {
   output.push_back(static_cast<char>(base64_table[array[i] >> 2]));
   output.push_back(static_cast<char>(base64_table[((array[i++] & 0x03) << 4) | ((array[i] & 0xF0) >> 4)]));
   output.push_back(static_cast<char>(base64_table[(array[i] & 0x0F) << 2]));
   output.push_back(static_cast<char>('='));
  }
  else if ((size - i) == 2)
  {
   output.push_back(static_cast<char>(base64_table[array[i] >> 2]));
   output.push_back(static_cast<char>(base64_table[(array[i] & 0x03) << 4]));
   output.push_back('=');
   output.push_back('=');
  }
 }

 return output;
}

std::vector<byte> Base64Decode(const std::string &array) // TODO
{
 const char *base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}

main.cpp: main.cpp中:

#include <iostream>
#include <vector>
#include "base64.h"

using namespace std;

int main(int argc, char *argv[])
{
 const char* prueba = "sure.";
 vector<byte> texto;
 string codificado;

 for (unsigned int i = 0; (prueba[i] != 0); i++)
 {
  texto.push_back(prueba[i]);
 }

 codificado = Base64Encode(texto);

 cout << codificado;

 return 0;
}

PD: Sorry for my bad knowledge of English :P PD:对不起,我对英语的不了解:P

You're missing a const in the implementation of Base64Encode , it's declared as: 您在Base64Encode的实现中缺少const ,它声明为:

std::string Base64Encode(const std::vector<byte> &array);

But implemented as 但实现为

std::string Base64Encode(std::vector<byte> &array) { ... }

The compiler thinks that you've overloaded the function (for const and non-const vectors) and thinks the implementation of the const version is missing. 编译器认为您已经重载了该函数(对于const和非const向量),并认为缺少const版本的实现。

Your header declares Base64Encode to accept the type const std::vector<byte> &array while your source file declares it to accept the type std::vector<byte> &array -- notice the missing const qualifier. 您的标头声明Base64Encode接受const std::vector<byte> &array而源文件声明它接受std::vector<byte> &array -注意缺少的const限定符。

So while base64.cpp compiles correctly, it doesn't get linked to the declaration in the header, which is the only prototype that main.cpp is aware of. 因此,尽管base64.cpp正确编译,但它不会链接到标头中的声明,而标头是main.cpp知道的唯一原型。 When the linker runs, main is looking for the signature with the const qualifier, but it's nowhere to be found. 链接器运行时, main正在使用const限定符查找签名,但找不到它。

Base64Encode的参数在标头中声明为const ,但在.cpp文件中声明为非const。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM