Note that there are some explanatory texts on larger screens.

plurals
  1. PONested templates gcc compiler 4.1.2 error
    text
    copied!<p>I'm trying to create a template class to insulate the users from a data type. I would have preferred to use an adapter class, but the function signatures needed to change requiring a template.</p> <p>In the code sample below(not the actual project just a simplified version to illustrate the problem), while in the main routine I'm able to use the ob_traits interface. But when I attempt to create the templated StructWrapper which uses the ob_traits as a base class, I get errors and gcc doesn't recognize the IntAdapter class created. This compiles on MSVC 8.0 but fails on gcc 4.1.2 20070626 ( Red hat 4.1.2-14)</p> <p>So two questions first, do you understand why the compile fails with the errors specified below?</p> <p>Second, any suggestions on how to implement this concept in a more simple manner?</p> <pre><code> #include &lt;iostream&gt; template &lt;typename T &gt; struct ob_traits { ob_traits( T&amp; param ) { value = param; }; T value; }; struct GeneralStructure { int a; GeneralStructure(int param):a(param){} }; struct DifferentStructure { GeneralStructure hidden; DifferentStructure( int param ):hidden(param){}; } ; /*template&lt; typename T &gt; struct ob_traits { }; */ template&lt;&gt; struct ob_traits&lt; GeneralStructure &gt; { struct IntAdapter { IntAdapter( GeneralStructure&amp; valueParam ):value(valueParam){} GeneralStructure value; int&amp; getValue() { return value.a; }; }; }; template&lt;&gt; struct ob_traits&lt; DifferentStructure &gt; { struct IntAdapter { IntAdapter( DifferentStructure&amp; valueParam):value( valueParam ){} DifferentStructure value; int&amp; getValue( ){ return value.hidden.a; }; }; void dump() { DifferentStructure testLocal(44); IntAdapter local( testLocal ); std::cout &lt;&lt; local.getValue()&lt;&lt;std::endl; } }; template &lt;typename T &gt; struct StructWrapper:public ob_traits&lt; T &gt; { StructWrapper(){}; /*main.cpp:60: error: 'IntAdapter' was not declared in this scope main.cpp:60: error: expected `;' before 'inner' main.cpp:60: error: 'inner' was not declared in this scope */ void dumpOuter(const T&amp; tempParam) { IntAdapter inner(tempParam); inner.dump(); }; /* main.cpp: In member function 'void StructWrapper&lt;T&gt;::dumpOuterFailsAsWell(const T&amp;)': main.cpp:66: error: expected `;' before 'inner' main.cpp:66: error: 'inner' was not declared in this scope */ void dumpOuterFailsAsWell(const T&amp; tempParam) { ob_traits&lt;T&gt;::IntAdapter inner(tempParam); inner.dump(); }; }; int main(int argc, char* argv[]) { GeneralStructure dummyGeneral(22); ob_traits&lt;struct GeneralStructure &gt;::IntAdapter test(dummyGeneral); DifferentStructure dummyDifferent(33); ob_traits&lt;struct DifferentStructure &gt;::IntAdapter test2(dummyDifferent); std::cout &lt;&lt; "GeneralStructure: "&lt;&lt;test.getValue()&lt;&lt;std::endl; std::cout &lt;&lt; "DifferentStructure: "&lt;&lt;test2.getValue()&lt;&lt;std::endl; ob_traits&lt;struct DifferentStructure &gt; test3; test3.dump(); std::cout &lt;&lt; "Test Templated\n"; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload