0

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*****/

  • 1
    Please add the necessary code to make your code a [mcve]. Also, please post the error messages, not the error codes. – R Sahu Dec 24 '18 at 04:09
  • Adding the text of the error messages to the question would make it easier to diagnose. – 1201ProgramAlarm Dec 24 '18 at 04:23
  • You can't just assume `std::size_t` is present, you need to include `cstddef` to get a definition for it. Then you can use `std::size_t`. VTC as a trivial error. – StoryTeller - Unslander Monica Dec 24 '18 at 06:35
  • Im confused. size_t is defined in stdio, stdlib and several others. Also, if that were the issue, then it would not work with the int return type, and yet it does. right? – user9201794 Dec 24 '18 at 17:24

0 Answers0