3 Replies - 586 Views - Last Post: 09 July 2010 - 06:04 PM Rate Topic: -----

#1 Rekan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 19-August 09

how to use class in c++

Posted 09 July 2010 - 12:50 AM

hi guys i am having a problem with using my class in c++.
i have written my class code as a header file and ihave included it in the source code

i get the compiler error:
1>extended.obj : error LNK2019: unresolved external symbol "class Token __cdecl get_token(void)" (?get_token@@YA?AVToken@@XZ) referenced in function _main
1>C:\Users\Minh-Long\documents\visual studio 2010\Projects\calculator (console type)\Debug\calculator (console type).exe : fatal error LNK1120: 1 unresolved externals

Is This A Good Question/Topic? 0
  • +

Replies To: how to use class in c++

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: how to use class in c++

Posted 09 July 2010 - 12:53 AM

The Compiler can't find the body of get_token function. It is prototyped somewhere, but it is not implemented.
Was This Post Helpful? 0
  • +
  • -

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: how to use class in c++

Posted 09 July 2010 - 05:42 PM

Usually, you will see classes defined similarly:
class foo
{
public:
    foo();
    ~foo();

    void method();

private:
    int member;
};



In C++, it is good practice to encapsulate the details of things. However, the implementation of the functions are not implemented yet. Failing to implement a function that is being used will result to an error.

foo::foo():
    member( 0 )
{
    // Do something.
}

foo::~foo()
{
    // Do something.
}

void foo::method()
{
    // do something.
}



Of course, you could always include the implementation inside the class definition:
class foo
{
public:
    foo()
    {
        // do something.
    }
    ~foo()
    {
        // do something.
    }

    void method()
    {
        // do something.
    }

private:
    int member;
};



The reason you don't see this done much in C++ is because it does not hide the implementation. Most of the time, people using classes written by someone else does not care about the implementation of the methods. They just care what it does, but not how it does it.

This post has been edited by sparkart: 09 July 2010 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: how to use class in c++

Posted 09 July 2010 - 06:04 PM

Especially since all you will usually be provided in the header and accompanying libraries, object files, or whatever.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1