Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error while using static int for incremental record.

Error while using static int for incremental record.

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    here in the code below i am trying to print the increment every time the same object is called, but it's giving error.

    #include
    using namespace std;

    class Names{
    int m;
    static int count;
    public:
    void setcode(void){
    m = ++count;
    cout<

    Error:

    Quote:

    /tmp/ccDcxr5a.o: In function Names::setcode()':
    savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count'
    savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
    savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count'
    collect2: error: ld returned 1 exit status

    Thank you

    L 1 Reply Last reply
    0
    • T Tarun Jha

      here in the code below i am trying to print the increment every time the same object is called, but it's giving error.

      #include
      using namespace std;

      class Names{
      int m;
      static int count;
      public:
      void setcode(void){
      m = ++count;
      cout<

      Error:

      Quote:

      /tmp/ccDcxr5a.o: In function Names::setcode()':
      savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count'
      savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
      savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count'
      collect2: error: ld returned 1 exit status

      Thank you

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Tarun Jha wrote:

      but it's giving error.

      And we must guess what that error is, and where it occurs?

      T 1 Reply Last reply
      0
      • L Lost User

        Tarun Jha wrote:

        but it's giving error.

        And we must guess what that error is, and where it occurs?

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        this is what compiler is showing:

        Quote:

        /tmp/ccDcxr5a.o: In function Names::setcode()':
        savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count' savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
        savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count' collect2: error: ld returned 1 exit status

        L 1 Reply Last reply
        0
        • T Tarun Jha

          this is what compiler is showing:

          Quote:

          /tmp/ccDcxr5a.o: In function Names::setcode()':
          savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count' savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
          savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count' collect2: error: ld returned 1 exit status

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You have declared the variable count in your Names class, but never defined it in your code. Add the following line after the class declaration:

          int Names::count;

          C 1 Reply Last reply
          0
          • L Lost User

            You have declared the variable count in your Names class, but never defined it in your code. Add the following line after the class declaration:

            int Names::count;

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            :thumbsup:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups