Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ defining constructor from header
    text
    copied!<p><strong>class_one.h</strong>:</p> <pre><code>#ifndef CLASS_ONE #define CLASS_ONE #include &lt;string&gt; namespace ones{ typedef enum{BLACK, WHITE, RED} b_color; typedef char b_letter; const b_letter letters[4] = {'A', 'B', 'C', 'D'}; class one{ b_color color; b_letter letter; public: one(b_color, b_letter); std::string combo(); b_color getColor(); b_letter getLetter(); }; } #endif </code></pre> <p>Given this header file, how should I go about creating the .cpp file, and how then instantiate this class in another file, main.cpp? I would think something like this:</p> <p><strong>class_one.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include "class_one.h" using namespace ones; class one { b_color color; b_letter letter; public: one(b_color c, b_letter l) //Not sure about this one.. { color = c; letter = l; } std::string combo() { return "blahblah temporary. letter: " + letter; //not finished } b_color getColor() { return color; } b_letter getLetter() { return letter; } }; </code></pre> <p>and then to instantiate it, I would do something like this:</p> <p><strong>main.cpp</strong></p> <pre><code>#include "class_one.h" int main() { ones::one test(ones::BLACK, ones::letters[0]); //cout&lt;&lt;test.name()&lt;&lt;endl; return 0; } </code></pre> <p>Everything is extracted from a larger cluster of files, but this is the essentials of my question.. The header file should be correct, but I'm not sure how to instantiate the 'one' class, and not with that constructor. I think the constructor I defined in the .cpp is wrong. I'm used to Java, so I've never seen a constructor like the one in the header file, if it's even a constructor. To me it looks like <code>method(int, int)</code> instead of what I'm used to: <code>method(int a, int b)</code> When running this I get this error:</p> <pre><code>main.obj : error LNK2019: unresolved external symbol "public: __thiscall ones::one::one(enum ones::b_color, char)" (??0one@ones@@QAE@W4b_color@1@D@Z) referenced in function _main &lt;path&gt;/project.exe : fatal error LNK1120: 1 unresolved externals </code></pre> <p>Sorry for the incredibly stupid naming I have here, but it does make sense for the purpose. May be some typing errors in the question codes as I've written most of this by hand right now. Any help appreciated..</p>
 

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