0

I searched for my problem for more than 3 hours but couldn't find any good articles about so decided to post a question here to ask for your advice. My problem is I don't understand how class works. It is just a simple class but in one case it doesn't work. First, below is the code that works


class R{
    int x, y;
public:
    R();
    R(int a, int b) : x(a), y(b) {};
    void setX(int a){ x = a; }
    void setY(int b){ y = b; }
    int getX(){ return x; }
    int getY(){ return y; }
    void display();
};
void R::display(){
// displaying x and y I removed the function because it somehow remove the rest of the code after arrow operator
}
int main(){
    R r(2,3);
    r.setX(1);
    r.setY(2);
    r.display();
}

but when I change the main like this it doesn't work


int main(){
    R r;
    r.setX(1);
    r.setY(2);
    r.display();
}

I first thought the reason might be that I didn't actually created an instance but found a code that works fine from a website. The code was like this:


// overloading operators example
#include 
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int a,int b) : x(a), y(b) {}
    CVector operator + (const CVector&);
};

CVector CVector::operator+ (const CVector& param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return temp;
}

int main () {
  CVector foo (3,1);
  CVector bar (1,2);
  CVector result;
  result = foo + bar;
// displaying x and y I removed the function because it somehow remove the rest of the code after arrow operator
  return 0;
}

It also just made an instance "CVector temp" not "CVector temp(2,3)" but it works fine while mine is not working.

If anyone knows what I am missing and the problem of my code, could you please give me your advice?

Thanks for reading and your time

June
  • 265
  • 1
  • 8
  • 21
  • Possible duplicate of http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – πάντα ῥεῖ May 11 '14 at 16:25
  • What is the error text? "Unsresolved external symbol `` in ``..." – smiling_nameless May 11 '14 at 16:26
  • Okay, I found out that it works if I make the constructor definition with braces like "R(){};" but still don't get why it needs here. If anyone knows it, could you teach me? – June May 11 '14 at 16:26
  • Thanks for your quick comments! @Max: I just noticed that some codes were unable to show when I typed << operators so modified it just right now. Um, but what do you mean the and ? @ πάντα ῥεῖ, thanks for your comment! I'm reading it now! – June May 11 '14 at 16:34
  • Because you need the default constructor, so you need a definition. Currently you only have a declaration. If it doesn't do anything, you can also remove it, and the compiler will generate one for you. – juanchopanza May 11 '14 at 16:34
  • @June It's likely irrelevant; see my answer. – smiling_nameless May 11 '14 at 16:36
  • Also irrelevant, but please make main return 0. – Banex May 11 '14 at 16:38
  • Thank you juanchopanza, πάντα ῥεῖ, and Banex! All of your advice actually helped me out! I was able to fix and even make it better with your advice! I really appreciate all of you who gave me good advice since every advice here was definitely helpful! Thank you all!!! – June May 11 '14 at 16:42

3 Answers3

2

From your comments, I can surmise that the error is related to the missing definition of R:: R ().

MSVC's Error LNK2019 refers to an unresolved symbol. A symbol is any function, object, variable, or type which has been declared but not defined.

In this case, I'm guessing that you have never provided a definition for R:: R (), but you wrote it in the class definition, which tells the compiler that it's there somewhere -- that's the declaration.

But, apparently, you never defined it. Adding {} provides the body of the function, which is why it works with those braces.

You could also define R:: R() as you did R:: display (), externally, or, if your compiler supports C++11, write explicitly R:: () = default.

Once you give the linker a body of the function, your code should link fine.

smiling_nameless
  • 1,047
  • 1
  • 9
  • 23
  • Thank you very much Max!! It really solved my problem and that was the answer I'd like to know! Thank you! Somehow it says I need to check this answer after 2mins so after that, I'll check this as an answer. Thank you! – June May 11 '14 at 16:37
1

When you write it like

R();

it is a Declaration of the constructor. You have to give each method you use a Definition also by giving it a body (even if the body is empty like here). For class method, either you provide the body as you did here inside the class, or you can define it separately in a source (.cpp) file like

R::R(){/*your code*/}

it is similar to the code for CVector CVector::operator+ as you posted.

Rakib
  • 7,435
  • 7
  • 29
  • 45
  • Thank you for your answer Rakibul! Though Max's had good exaplanation but I think your answer is simple and good enough to understand so I'll promote it! Thank you for your answer! Now I feel much better! – June May 11 '14 at 16:39
1

You NEED to have a default constructor.

santahopar
  • 2,933
  • 2
  • 29
  • 50
  • Thank you for your answer armanali! Now I figured out I must actually give it a definition. Thank you! – June May 11 '14 at 16:40