1

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

Sti
  • 8,275
  • 9
  • 62
  • 124

1 Answers1

2

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.

Publius
  • 1,184
  • 2
  • 10
  • 27
  • Oh, I thought that was what `using namespace ones;` was doing for me in the .cpp file.. But okay, I tried that now, but "Visual C++ 2010 Express" gives me red lines and error under all the methods now, saying `Error: qualifier must be base class of "one"`. And the constructor is saying `Error: type used as constructor name does match type "one"`, and it gives me two different 'call examples' or something: `one::one(ones::b_color c, b_letter l)` and `one::one(const one &)`, which leads me to think that the first example is what I am trying to do, and the second is what it wants me to call? – Sti Feb 19 '13 at 02:05
  • My bad, should be ones::one::b_color and ones::one::b_letter. Or you can do "using namespaces ones;" and I think that should work if you remove the "ones::" in the cpp file. Edit my answer to reflect that. – Publius Feb 19 '13 at 02:11
  • Actually it only cleans up if I remove the `::one` from everything, I guess it's because the methods are IN `class one {}`. But this does not solve the original problem.. The problem is that the .h file SHOULD be complete, but the constructor there looks ridiculous to me, and I don't know how to define it in the .cpp-file. As I said, in the header it says `public: one(b_color, b_letter);` instead of `public: one(b_color c, b_letter l);` for the constructor, and I don't know what that means.. The header file SHOULD be correct, so I don't want to change it.. – Sti Feb 19 '13 at 02:35
  • Either is valid for the function declaration. Your constructor should work declared as I showed above. Post the entirety of your CPP file code. – Publius Feb 19 '13 at 04:30
  • I tried creating a fresh project, with only the three classes I posted above, still the same error. Tried with different compilers as well, just in case. Different logs for the error though, unix g++ said "main.cpp: Undefined referance to 'ones::one::one(ones::b_color, char)'". This means I am calling that spesific method in my main.cpp, but it can't find it? I even typecasted the char to be a b_letter, but it stays a char, but I don't think that's the problem.. – Sti Feb 19 '13 at 11:18
  • I thought you only had one class. – Publius Feb 19 '13 at 18:16
  • Well, 1 class (one), but three files. Called classes in my other languages.. Old and bad habit to call every file a class I guess.. .h, .cpp and main.cpp. – Sti Feb 19 '13 at 21:36
  • Okay, you're going to have to post your CPP code. I using the style of implementation I posted and it worked. – Publius Feb 20 '13 at 03:43
  • 1
    Ah, now I read you answer carefully, and I seem to have misunderstood what you meant by 'redefining the class block'. Previously I thought my only problem was my method names which needed the ones::one:: etc. When you provided that part of your answer I guess I forgot to read on. I removed the extra class block and other redefinitions, and now it works! Thank you. Sorry for my incompetence. – Sti Feb 20 '13 at 15:28