Search code examples
c++compiler-errorsinstantiationunresolved-externallnk2019

C++ defining constructor from header


class_one.h:

#ifndef CLASS_ONE
#define CLASS_ONE
#include <string>

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

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:

class_one.cpp

#include <iostream>
#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;
    }
};

and then to instantiate it, I would do something like this:

main.cpp

#include "class_one.h"

int main()
{
    ones::one test(ones::BLACK, ones::letters[0]);
    //cout<<test.name()<<endl;
    return 0;
}

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 method(int, int) instead of what I'm used to: method(int a, int b) When running this I get this error:

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
<path>/project.exe : fatal error LNK1120: 1 unresolved externals

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..


Solution

  • Your cpp file should look like this:

    #include "class_one.h"
    
    ones::one::one(ones::one::b_color c, ones::one::b_color l)
    {
        //code here
    }
    
    std::string ones::one::combo()
    {
       // code here
    }
    
    // additional functions...
    

    And so on. You don't redefine the class with a class block, you just specify the individual function definitions like I showed here. The function definition format should be something like this:

    [return type] [namespace]::[class]::[function]([parameters])
    {
        // code here
    }
    

    It looks like you're good on instantiation. You also don't have to redeclare the member variables.