My issue is simple. I am able to declare an overloaded function in the global namespace as a friend of a template class in a nested namespace using fundamental types, however if I change the definition and declaration of the function to return size_t, I get C2510 and C4430 errors. This only occurs with the return type of the function. size_t in the parameter list does not generate the issue. I have tried ::size_t, ::std::size_t, and std::size_t as well. Nothing seems to work. What is going on here and why?
I am using MS Visual Studio 2015
Thanks
UPDATED: Per comments I have included remaining code TestingApp.cpp. I also included stddef which does not solve the problem. With respect to the specific error messages I received, they are:
C2510 'size_t': left of '::' must be a class/struct/union TestingApp
pointer.h 41
C4430 missing type specifier - int assumed. Note: C++ does not support
default-int TestingApp pointer.h 41
Line 41 that does NOT generate compilation errors:
friend int ::fwrite<DATA_TYPE>(const Pointer<DATA_TYPE>& ptr, size_t count, FILE* stream);
Line 41 that DOES generate compilation errors:
friend size_t ::fwrite<DATA_TYPE>(const Pointer<DATA_TYPE>& ptr, size_t count, FILE* stream);
The following two files compile and run with no problem. However if I substitute int with size_t for the return type (and ONLY the return type keeping size_t as the second parameter of the function in either case) in the forward declaration, friend declaration, and definition, I get the errors indicated above.
/*******Begin Pointer.h******/
#pragma once
#include <cstdio>
namespace FW{
namespace Memory {
template <typename DATA_TYPE> class Pointer;
}
}
template <typename DATA_TYPE> inline
/* int works, size_t does not work */
int
/* size_t */
fwrite(const
FW::Memory::Pointer<DATA_TYPE>& ptr, size_t count, FILE* stream);
namespace FW{
namespace Memory{
template <typename DATA_TYPE> class Pointer {
/*Line 41*/ friend
/* int works, size_t does not work */
int
/* size_t */
::fwrite<DATA_TYPE>(const Pointer<DATA_TYPE>& ptr, size_t count, FILE* stream);
public:
/* Omitted for brevity */
private:
DATA_TYPE* m_pCurrent;
};
}
}
template <typename DATA_TYPE>
/* int works, size_t does not work */
int
/* size_t */
fwrite<DATA_TYPE>(const FW::Memory::Pointer<DATA_TYPE>& ptr, size_t count, FILE* stream) {
return fwrite(ptr.m_pCurrent, sizeof(DATA_TYPE), count, stream);
}
/*******End Pointer.h*******/
/*******Begin TestingApp.cpp******/
// TestingApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstdio>
#include <cstddef>
#include "..\DB\Pointer.h"
int main()
{
unsigned long* pData = new unsigned long[100];
for (size_t i = 0; i < 100; i++) {
pData[i] = i;
}
FW::Memory::Pointer<unsigned long> ptr(pData);
FILE* pFile = fopen("testFile", "wb");
if (pFile) {
fwrite(ptr, 10, pFile);
fclose(pFile);
}
delete[] pData;
pData = 0;
return 0;
}
/*****End TestingApp.cpp*****/