-3

So, I have a definition in my header file:

std::vector<char> showBytes(char const* fileName);

And it keeps giving me this error when I try to compile the program:

error: 'vector' in namespace 'std' does not name a template type

Any clue why it's giving me this?

Edit:

#include <vector>
#include "file.h"

std::vector<char> showBytes(char const* fileName) {
    std::ifstream ifs(fileName, std::ios::binary|std::ios::ate);
    std::ifstream::pos_type pos = ifs.tellg();

    std::vector<char> result(pos);

    ifs.seekg(0, std::ios::beg);
    ifs.read(&result[0], pos);

    return result;
}

File.h

std::vector<char> showBytes(char const* fileName);
John G
  • 1
  • 1
  • 1
  • 2

1 Answers1

1

A header file provides the interface of the functions declared within it. The parameter types, as well as return type, should have at least been declared when encountered. By default <vector> is not included (which holds the declaration and definition), nor have you forward declared it. This results in the return type of your function to be unknown, making it hard for the compiler to determine how much space should be reserved on the stack for the return value.

By including <vector> before your header file in each compilation unit (c++ file) that uses it, the declaration should be present in that compilation unit. This however is rather unwielding. The same result can however be achieved by simply including in the header file, this way you are not dependant on the order of inclusion.

File.h:

#ifndef FILE_H
#define FILE_H
#include <vector>
std::vector<char> showBytes(char const* fileName);
#endif // FILE_H

On another note, you might also want to include <fstream> in your File.cpp; it contains the declaration for ifstream. An include guard (also knows as a header guard) is currently not necessary, but I added it regardless since it can save you some trouble.

WedaPashi
  • 3,561
  • 26
  • 42
Yemachu
  • 144
  • 5
  • 3
    Note that names that begin with an underscore followed by a capital letter (`_FILE_H_`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker May 02 '17 at 12:17
  • @PeteBecker While I vaguely remembered reading something akin to that sometime prior to your comment, it has never given me issues yet. Therefore it didn't quite come to mind when I wrote the answer. In any case, I have updated the answer wih your suggestion. – Yemachu May 18 '17 at 08:29