Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use a class Y defined later in the code inside class X defined earlier?
    primarykey
    data
    text
    <p>I have this code and it compiles successfully.</p> <pre><code>#include &lt;iostream&gt; #include &lt;memory&gt; template &lt;typename T&gt; class Expression { public: T _data; Expression() {} Expression(T data) { _data = data; } void foo(); }; template &lt;typename T&gt; class ConstExpression : public Expression&lt;T&gt; { public: ConstExpression() {} ConstExpression(T data) { this-&gt;_data = data; } }; template &lt;typename T&gt; void Expression&lt;T&gt;::foo() { std::shared_ptr&lt;ConstExpression&lt;T&gt;&gt; temp(new Expression); std::shared_ptr&lt;ConstExpression&lt;T&gt;&gt; temp2(new ConstExpression&lt;T&gt;()); ConstExpression&lt;T&gt; foo2(5); } int main() { return 0; } </code></pre> <p>Now if I do the following or give my function foo an input argument of type constexpression I get an error: main.cc:15:25: error: use of undeclared identifier 'ConstExpression'; did you mean 'Expression'? In my real code my function takes an input argument of type ConstExpression and I have done an out of line declaration too but get a similar error.</p> <pre><code>#include &lt;iostream&gt; #include &lt;memory&gt; template &lt;typename T&gt; class Expression { public: T _data; Expression() {} Expression(T data) { _data = data; } void foo() { std::shared_ptr&lt;ConstExpression&lt;T&gt;&gt; temp(new Expression); std::shared_ptr&lt;ConstExpression&lt;T&gt;&gt; temp2(new ConstExpression&lt;T&gt;()); ConstExpression&lt;T&gt; foo2(5); } }; template &lt;typename T&gt; class ConstExpression : public Expression&lt;T&gt; { public: ConstExpression() {} ConstExpression(T data) { this-&gt;_data = data; } }; int main() { return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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