What am I doing wrong ?, c ++ reference

I am trying to code a simple base64 encoder / decoder (to test my programming skills).

I can compile it but it is not linked, I have an error message:

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 &) '

collect2: ld returned 1 exit status

Compiler and Linker: Mingw32 3.4.5 SO: Windows XP

This is my original code:

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:

#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:

#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 poor English skills: P

+2


a source to share


3 answers


You are missing const

in the implementation Base64Encode

, it is declared as:

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

      

But implemented as



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

      

The compiler thinks you have overloaded the function (for constant and non-constant vectors) and thinks there is no implementation of the const version.

+4


a source


Your header declares Base64Encode to accept a type const std::vector<byte> &array

, while your source file declares it to accept a type std::vector<byte> &array

- notices the missing qualifier const

.



So, if base64.cpp

compiled correctly, it is not tied to the declaration in the header, which is the only prototype it knows about main.cpp

. When the linker is running, it main

looks for a qualified signature const

, but it is nowhere to be found.

+2


a source


The argument is Base64Encode

declared as const

in the header, but not const in the .cpp file.

+1


a source







All Articles