0

I am following the cpluplus.com (c++) tutorial using 2 compilers, VisualStudio2017 and MinGW distro.

MinGW distro compiles the following code without errors, but VisualStudio2017 gives me an ambiguous error on the definition/initialization of the variable "size"(line 11). Why does this happen?

// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
if (file.is_open()){
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();
    cout << "the complete file content is in memory";
    delete[] memblock;
}else cout << "Unable to open file";
return 0;
}
BobMorane
  • 3,870
  • 3
  • 20
  • 42
Armando
  • 31
  • 4
  • 4
    Get rid of the `using namespace std;` because you are shadowing [`std::size`](http://en.cppreference.com/w/cpp/iterator/size), which is one of the reasons why [polluting your global namespace is bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Cory Kramer Apr 12 '18 at 16:39
  • 2
    @CoryKramer Shouldn't we just close this as a dupe of your link? – NathanOliver Apr 12 '18 at 16:44
  • 1
    error C2872: 'size': ambiguous symbol, could be `std::fpos<_Mbstatet> size`, could be `size`. Library oops, probably should have been `_size`. Use Help > Send Feedback > Report a Problem to let them know. Workaround is simple, make it a local variable or pick a different identifier name like `fsize`. – Hans Passant Apr 12 '18 at 16:49
  • @HansPassant The conflict is between the global `size` variable declared by OP and the `template constexpr size_t size(const T (&array)[N]) noexcept` function declared in ``. So not a library symbol naming issue. – 1201ProgramAlarm Apr 12 '18 at 17:07

0 Answers0