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. Declaring Class function error?

Declaring Class function error?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++debuggingquestionworkspace
3 Posts 2 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.
  • A Offline
    A Offline
    Aidman
    wrote on last edited by
    #1

    Hi all, :) I am experiencing a strange problem when declaring classes outside the main source file in VC++. I have made an experiment that demonstrates my problem. There are two files: “main.cpp” and “class.cpp”. “main.cpp” content:

    #include "class.cpp"

    int main() {
    return 0;
    }

    “class.cpp” content:

    class Class {
    public:
    void Test();
    };

    void Class::Test() {
    return;
    }

    When I try to compile I get following error: --------------------Configuration: main - Win32 Debug-------------------- Compiling... main.cpp class.cpp Linking... class.obj : error LNK2005: "public: void __thiscall Class::Test(void)" (?Test@Class@@QAEXXZ) already defined in main.obj Debug/main.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. main.exe - 2 error(s), 0 warning(s) It says that the function void Test() is already declared, but how? Please help me, am I doing something wrong? :( Aidman » over and out

    A 1 Reply Last reply
    0
    • A Aidman

      Hi all, :) I am experiencing a strange problem when declaring classes outside the main source file in VC++. I have made an experiment that demonstrates my problem. There are two files: “main.cpp” and “class.cpp”. “main.cpp” content:

      #include "class.cpp"

      int main() {
      return 0;
      }

      “class.cpp” content:

      class Class {
      public:
      void Test();
      };

      void Class::Test() {
      return;
      }

      When I try to compile I get following error: --------------------Configuration: main - Win32 Debug-------------------- Compiling... main.cpp class.cpp Linking... class.obj : error LNK2005: "public: void __thiscall Class::Test(void)" (?Test@Class@@QAEXXZ) already defined in main.obj Debug/main.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. main.exe - 2 error(s), 0 warning(s) It says that the function void Test() is already declared, but how? Please help me, am I doing something wrong? :( Aidman » over and out

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      pretty simple to fix: the class prototype should be in a header called class.h The class implementation (the class' function definitions) should be in class.cpp. Both main.cpp and class.cpp should #include "class.h" ;-)

      A 1 Reply Last reply
      0
      • A Anonymous

        pretty simple to fix: the class prototype should be in a header called class.h The class implementation (the class' function definitions) should be in class.cpp. Both main.cpp and class.cpp should #include "class.h" ;-)

        A Offline
        A Offline
        Aidman
        wrote on last edited by
        #3

        Great! Thanks alot :) Aidman » over and out

        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