| | |
hi
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
template <class T> void trace(LogBuffer& msgBuffer, T& data) { data.trace(msgBuffer); }
error:request for member 'trace' in 'data', which is of non-class type 'std::ios_base& ()(std::ios_base&)
The trace function is being called in some other file as shown below
C++ Syntax (Toggle Plain Text)
template <class T> LogBuffer& LogBuffer::operator<<(T & data) { trace(*this, data); // trace fuction called here return *this; }
We found that the above piece of code compiles fine on older version of gcc compiler like 2.9.
//Trace functions. These are the trace functions called form here we able to call different trace functions of different classes (due to the template deceleration)
//Again these trace functions are at different class of different files.
// here past compiler simply forget the problem of generic type. But GCC 4.1.2 compiler very much specific about this generic types.
// I hope that u can understand with this info..
this is the class which we have
C++ Syntax (Toggle Plain Text)
class LogBuffer { public: LogBuffer(LogFactor& logFactor); ~LogBuffer(); uint8_t* GetStart(); size_t GetProcessedSize(); void Flush(); void SetFormat(ios::fmtflags flags, ios::fmtflags mask); uint8_t GetIndent(); void SetIndent(uint8_t indent); // Generic flat type template <class T> LogBuffer& operator<<(T& data); // Single element pointer template <class T> LogBuffer& operator<<(T* & data); // Array template <class T> LogBuffer& operator<<(const MsgBufferArray<T>& data); protected: void SetDefaultFormat(); LoggerFunctor& _functor; ostringstream _buffer; uint8_t _indent; };
and also that some of the definitions at different file are available in the frm of class members.
This template function is unable to resolve the type.
any suggestions....
|
Last edited by Ancient Dragon; Nov 10th, 2008 at 9:26 am. Reason: add code tags
I tried to remake the program and provide examples in an attempt to reproduce the problem on Microsoft Visual C++ 2005/2008 compiler, but I different errors than the one I expected despite the tests--
Similar Problem here
Perhaps the errors differ from compiler to compiler, or it may be possible that the examples provided above were not adjacent to the example in the link?
The error seems to occur in gcc (and/or g++?) type compilers so I think the error stated above may be the same thing though I can't confirm it without experience with those compilers.
Similar Problem here
c++ Syntax (Toggle Plain Text)
/** * Assumed header file for LogBuffer */ #ifndef LOGBUFFER_H #define LOGBUFFER_H #include <iostream> /* Dummy values */ typedef char LogFactor; typedef int uint8_t; template<class T>class MsgBufferArray; typedef char LoggerFunctor; typedef char ostringstream; using std::ios; namespace Log{ class LogBuffer; template <class T> void trace(LogBuffer& msgBuffer, T& data); class LogBuffer{ public: LogBuffer(LogFactor& logFactor); ~LogBuffer(); uint8_t* GetStart(); size_t GetProcessedSize(); void Flush(); void SetFormat(ios::fmtflags flags, ios::fmtflags mask); uint8_t GetIndent(); void SetIndent(uint8_t indent); // Generic flat type template <class T> LogBuffer& operator<<(T& data); // Single element pointer template <class T> LogBuffer& operator<<(T* & data); // Array template <class T> LogBuffer& operator<<(const MsgBufferArray<T>& data); protected: void SetDefaultFormat(); //LoggerFunctor& _functor; ostringstream _buffer; uint8_t _indent; }; } #include "LogBuffer.cpp" #endif
c++ Syntax (Toggle Plain Text)
/** * Assumed implementation file of LogBuffer */ #ifdef LOGBUFFER_H using namespace Log; LogBuffer::LogBuffer(LogFactor& logFactor){} LogBuffer::~LogBuffer(){} template <class T> LogBuffer& LogBuffer::operator<<(T & data){ ::trace(*this, data); // trace fuction called here return *this; } template <class T> void trace(LogBuffer& msgBuffer, T& data){ data.trace(msgBuffer); } #endif
c++ Syntax (Toggle Plain Text)
/** * Driver program for testing-- */ #include <iostream> #include "LogBuffer.h" using std::cout; using std::cin; using std::endl; class Tracer{ public: Tracer(){} void trace(LogBuffer& msgBuffer){ cout << "Trace successful" << endl; } }; class NonTracer{ public: NonTracer(){} char trace; }; int main(){ char refChar = '\0'; // dummy variable Tracer myTracer; // object of type Tracer, has the method trace NonTracer myNonTracer; // object of type NonTracer, doesn't have trace function but a trace mbmer Tracer myOtherTracer(); // pointer to a function that accepts no arguments and returns a Tracer LogBuffer lb (refChar); // making a LogBuffer object and passing it the dummy variable lb << myTracer; // works // lb << myNonTracer; // obviously doesn't work, but doesn't reproduce desired error // lb << myOtherTracer; Error 1 error C2228: left of '.trace' must have class/struct/union h:\visual studio 2008\projects\testingtemplates__\testingtemplates__\tesitngtemplate__t.cpp 35 TestingTemplates__ // myOtherTracer.trace(LogBuffer(refChar)); Error 1 error C2228: left of '.trace' must have class/struct/union h:\visual studio 2008\projects\testingtemplates__\testingtemplates__\tesitngtemplate__t.cpp 35 TestingTemplates__ cin.ignore(); cin.get(); return 0; }
Perhaps the errors differ from compiler to compiler, or it may be possible that the examples provided above were not adjacent to the example in the link?
The error seems to occur in gcc (and/or g++?) type compilers so I think the error stated above may be the same thing though I can't confirm it without experience with those compilers.
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
this is my mini project of testing the code at different compilers I am a learner of testing. My master starts with this task. but he also struck with this point.
Now I clearly explain the scenario.
compiling fib_msg.cpp . It is including header files like fib_msg.h, hf_logger.h, hf_looger_impl.h and hf_list.h . I have taken preprocessor output for fib_msg.cpp. I found that all the files r included one after another.
step 1. In fib_msg.h
step 2. first is the hf_logger.h where u can see the function call on a definition of overloading operator function.
step 3. then we have hf_logger_impl.h
step 4. Finally with hf_list.h
// So now U can understand.
Now I clearly explain the scenario.
compiling fib_msg.cpp . It is including header files like fib_msg.h, hf_logger.h, hf_looger_impl.h and hf_list.h . I have taken preprocessor output for fib_msg.cpp. I found that all the files r included one after another.
step 1. In fib_msg.h
#include "hf_logger.h"
#include "hf_list.h
inline
void trace(HF_OLogBuffer& msgBuffer, fib_msg_reqHeader_t& data)
{
msgBuffer << "cbHandle=0x";
msgBuffer << hex << setw(8) << setfill('0') << data.cbHandle << endl; // error : instantiated from here
msgBuffer << "cbCorrelator=0x";
msgBuffer << hex << setw(8) << setfill('0') << data.cbCorrelator << endl;
msgBuffer << "errorReporting=";
msgBuffer << fib_trace_value(g_fib_trace_errorReportingTypes,
data.errorReporting,
"unknown type");
msgBuffer << endl;
msgBuffer << "msgSubId=" << data.msgSubId << endl;
}class LogBuffer
{
public:
LogBuffer(LoggerFunctor& loggerFunctor);
~LogBuffer();
uint8_t* GetStart();
size_t GetProcessedSize();
void Flush();
void SetFormat(ios::fmtflags flags, ios::fmtflags mask);
uint8_t GetIndent();
void SetIndent(uint8_t indent);
// Basic types
LogBuffer& operator<<(int8_t& data); // a character, not a number
LogBuffer& operator<<(const int8_t& data); // a character, not a number
LogBuffer& operator<<(uint8_t& data); // a character, not a number
LogBuffer& operator<<(const uint8_t& data); // a character, not a number
// C strings
LogBuffer& operator<<(const char* data);
LogBuffer& operator<<(char data[]);
// std::string
LogBuffer& operator<<(std::string& data);
LogBuffer& operator<<(const std::string& data);
// Generic flat type this function is current issue generator
template <class T>
LogBuffer& operator<<(T& data);
// Single element pointer
template <class T>
LogBuffer& operator<<(T* & data);
// Array
template <class T>
LogBuffer& operator<<(const MsgBufferArray<T>& data);
// Manipulators
LogBuffer& operator<<(ios& (*fct)(ios& iosArg));
};
template <class T>
void trace(HF_OLogBuffer& msgBuffer, T& data)
{
data.trace(msgBuffer);
}// this is the function call from which we got an error i.e. {request for member 'trace' in 'data', which is of non-class type 'std::ios_base& ()(std::ios_base&)'}
inline
void trace(HF_OLogBuffer& msgBuffer, HF_int8_t& data)
{
msgBuffer << data;
}//and so on trace functions definitions..step 3. then we have hf_logger_impl.h
template <class T>
inline
HF_OLogBuffer& HF_OLogBuffer::operator<<(T& data)
{
trace(*this, data);// error: instantiated from 'HF_OLogBuffer& HF_OLogBuffer::operator<<(T&) [with T = std::ios_base& ()(std::ios_base&)]'
return *this;
}step 4. Finally with hf_list.h
#include "hf_logger.h"
template <class T, class Alloc=BufferChunkAllocator>
class MsgBufferList
{
public:
MsgBufferList(Alloc& alloc) {init(alloc);}
void init(Alloc& alloc)
{
_n = 0;
_first = 0;
_last = 0;
_allocator = &alloc;
}
uint32_t size() {return _n;}
void trace(HF_OLogBuffer& msgBuffer)
{
MsgBufferListElem<T>* elemP = _first;
for (NPF_uint32_t i = 0; (i < _n) && elemP; i++)
{
msgBuffer << elemP;
elemP = elemP->_next;
}
}
uint32_t _n;
MsgBufferListElem<T>* _first;
MsgBufferListElem<T>* _last;
};// So now U can understand.
Last edited by sagar_d; Nov 11th, 2008 at 2:29 am.
I think the problem may exist in your hf_logger.h file--
Because you only have a 'specialization' for an ios type (and anything derived from it) and not its super type, you're generalized function is invoked instead of the one you most likely intended to be invoked.
That's my assumption, given the error generated. I only know so much about call-backs, but if what I'm thinking is true your member in fib_msg_reqHeader_t cbHandle is a pointer to a function that takes an ios_base reference and returns an ios_base reference and since you don't have a specialization for your callback the generic type is being called.
You could probably resolve the error by allowing your overloaded<< definition that takes a pointer to a function that accepts and returns an ios reference to instead allow pointers to a function that accepts and returns an ios_base.
c++ Syntax (Toggle Plain Text)
class LogBuffer { public: LogBuffer(LoggerFunctor& loggerFunctor); ~LogBuffer(); uint8_t* GetStart(); size_t GetProcessedSize(); void Flush(); void SetFormat(ios::fmtflags flags, ios::fmtflags mask); uint8_t GetIndent(); void SetIndent(uint8_t indent); // Basic types LogBuffer& operator<<(int8_t& data); // a character, not a number LogBuffer& operator<<(const int8_t& data); // a character, not a number LogBuffer& operator<<(uint8_t& data); // a character, not a number LogBuffer& operator<<(const uint8_t& data); // a character, not a number // C strings LogBuffer& operator<<(const char* data); LogBuffer& operator<<(char data[]); // std::string LogBuffer& operator<<(std::string& data); LogBuffer& operator<<(const std::string& data); // Generic flat type this function is current issue generator template <class T> LogBuffer& operator<<(T& data); // Single element pointer template <class T> LogBuffer& operator<<(T* & data); // Array template <class T> LogBuffer& operator<<(const MsgBufferArray<T>& data); // Manipulators // Takes a pointer to an ios that accepts an ios arg, ios_base LogBuffer& operator<<(ios& (*fct)(ios& iosArg)); }; template <class T> void trace(HF_OLogBuffer& msgBuffer, T& data) { data.trace(msgBuffer); }// this is the function call from which we got an error i.e. {request for member 'trace' in 'data', which is of non-class type 'std::ios_base& ()(std::ios_base&)'} inline void trace(HF_OLogBuffer& msgBuffer, HF_int8_t& data) { msgBuffer << data; }//and so on trace functions definitions..
Because you only have a 'specialization' for an ios type (and anything derived from it) and not its super type, you're generalized function is invoked instead of the one you most likely intended to be invoked.
That's my assumption, given the error generated. I only know so much about call-backs, but if what I'm thinking is true your member in fib_msg_reqHeader_t cbHandle is a pointer to a function that takes an ios_base reference and returns an ios_base reference and since you don't have a specialization for your callback the generic type is being called.
You could probably resolve the error by allowing your overloaded<< definition that takes a pointer to a function that accepts and returns an ios reference to instead allow pointers to a function that accepts and returns an ios_base.
•
•
Join Date: Nov 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
I think the problem may exist in your hf_logger.h file--
c++ Syntax (Toggle Plain Text)
class LogBuffer { public: LogBuffer(LoggerFunctor& loggerFunctor); ~LogBuffer(); uint8_t* GetStart(); size_t GetProcessedSize(); void Flush(); void SetFormat(ios::fmtflags flags, ios::fmtflags mask); uint8_t GetIndent(); void SetIndent(uint8_t indent); // Basic types LogBuffer& operator<<(int8_t& data); // a character, not a number LogBuffer& operator<<(const int8_t& data); // a character, not a number LogBuffer& operator<<(uint8_t& data); // a character, not a number LogBuffer& operator<<(const uint8_t& data); // a character, not a number // C strings LogBuffer& operator<<(const char* data); LogBuffer& operator<<(char data[]); // std::string LogBuffer& operator<<(std::string& data); LogBuffer& operator<<(const std::string& data); // Generic flat type this function is current issue generator template <class T> LogBuffer& operator<<(T& data); // Single element pointer template <class T> LogBuffer& operator<<(T* & data); // Array template <class T> LogBuffer& operator<<(const MsgBufferArray<T>& data); // Manipulators // Takes a pointer to an ios that accepts an ios arg, ios_base LogBuffer& operator<<(ios& (*fct)(ios& iosArg)); }; template <class T> void trace(HF_OLogBuffer& msgBuffer, T& data) { data.trace(msgBuffer); }// this is the function call from which we got an error i.e. {request for member 'trace' in 'data', which is of non-class type 'std::ios_base& ()(std::ios_base&)'} inline void trace(HF_OLogBuffer& msgBuffer, HF_int8_t& data) { msgBuffer << data; }//and so on trace functions definitions..
Because you only have a 'specialization' for an ios type (and anything derived from it) and not its super type, you're generalized function is invoked instead of the one you most likely intended to be invoked.
That's my assumption, given the error generated. I only know so much about call-backs, but if what I'm thinking is true your member in fib_msg_reqHeader_t cbHandle is a pointer to a function that takes an ios_base reference and returns an ios_base reference and since you don't have a specialization for your callback the generic type is being called.
You could probably resolve the error by allowing your overloaded<< definition that takes a pointer to a function that accepts and returns an ios reference to instead allow pointers to a function that accepts and returns an ios_base.
error: this is the function call from which we got an error i.e. {request for member 'trace' in 'data', which is of non-class type 'std::ios_base& ()(std::ios_base&)'}
What I'm saying is change the operator prototype--
-- and definition to --
--but only if the operations within the operator will allow you to do so. Since ios_base is a super-type of ios I'm assuming that ios_base wont have the same functionality (or the same amount of members) as ios.
In the event that you do not want to change the definition of your operator for ios-types, change your cbHandle to accept and return a reference to an ios type instead of an ios_base.
--again only do this if it wont make any major changes in other portions of code. Consider each option carefully and if you find a solution you can simply add another stream-extraction operator to handle ios_base types.
c++ Syntax (Toggle Plain Text)
// Takes a pointer to an ios that accepts an ios arg, ios_base LogBuffer& operator<<(ios& (*fct)(ios& iosArg));
-- and definition to --
c++ Syntax (Toggle Plain Text)
// Takes a pointer to an ios that accepts an ios arg, ios_base LogBuffer& operator<<(ios_base& (*fct)(ios_base& iosArg));
--but only if the operations within the operator will allow you to do so. Since ios_base is a super-type of ios I'm assuming that ios_base wont have the same functionality (or the same amount of members) as ios.
In the event that you do not want to change the definition of your operator for ios-types, change your cbHandle to accept and return a reference to an ios type instead of an ios_base.
c++ Syntax (Toggle Plain Text)
/** * Possible definition of fib_msg_reqHeader_t before you decide to change it */ struct fib_msg_reqHeader_t{ //... ios_base& (*cbHandle)(ios_base&); //... };
c++ Syntax (Toggle Plain Text)
/** * Possible definition of fib_msg_reqHeader_t after you decide to change it */ struct fib_msg_reqHeader_t{ //... ios& (*cbHandle)(ios&); //... };
--again only do this if it wont make any major changes in other portions of code. Consider each option carefully and if you find a solution you can simply add another stream-extraction operator to handle ios_base types.
![]() |
Other Threads in the C++ Forum
- Previous Thread: cars using structures
- Next Thread: please help me do my asignment
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





