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. defining a string constant

defining a string constant

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomhelp
4 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.
  • K Offline
    K Offline
    K Shaffer
    wrote on last edited by
    #1

    I have a constants.h file that I would like to use to store constants and such. When I include the file in a class in my project I keep getting Link Error:2005: "already defined in" errors when I try to declare a string constant in the constant.h file. How do I create global string constants that I can use througout my app? // constants.h: defines constants to be used througout application. // ////////////////////////////////////////////////////////////////////// #if !defined(APP_CONSTANTS_0209051212) #define APP_CONSTANTS_0209051212 //no problems here const APP_PATH_STRING_LENGTH = 255; //link errors when using these char* app_data_path = "data\\"; char* app_charts_path = "c:\\CHARTS\\"; #endif // !defined(APP_CONSTANTS_0209051212) Kevin Shaffer kshaff03@msn.com

    K 1 Reply Last reply
    0
    • K K Shaffer

      I have a constants.h file that I would like to use to store constants and such. When I include the file in a class in my project I keep getting Link Error:2005: "already defined in" errors when I try to declare a string constant in the constant.h file. How do I create global string constants that I can use througout my app? // constants.h: defines constants to be used througout application. // ////////////////////////////////////////////////////////////////////// #if !defined(APP_CONSTANTS_0209051212) #define APP_CONSTANTS_0209051212 //no problems here const APP_PATH_STRING_LENGTH = 255; //link errors when using these char* app_data_path = "data\\"; char* app_charts_path = "c:\\CHARTS\\"; #endif // !defined(APP_CONSTANTS_0209051212) Kevin Shaffer kshaff03@msn.com

      K Offline
      K Offline
      K Shaffer
      wrote on last edited by
      #2

      I made the strings static, which appears to have solved my problem. Is this the best way to handled such a problem? char* app_data_path = "data\\"; char* app_charts_path = "c:\\CHARTS\\"; to: static char* app_data_path = "data\\"; static char* app_charts_path = "c:\\CHARTS\\"; Kevin Shaffer kshaff03@msn.com

      C 1 Reply Last reply
      0
      • K K Shaffer

        I made the strings static, which appears to have solved my problem. Is this the best way to handled such a problem? char* app_data_path = "data\\"; char* app_charts_path = "c:\\CHARTS\\"; to: static char* app_data_path = "data\\"; static char* app_charts_path = "c:\\CHARTS\\"; Kevin Shaffer kshaff03@msn.com

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        kshaff03 wrote: Is this the best way to handled such a problem? nope. the best way is like this: foo.cpp: #include "foo.h" const char* app_data_path = "data\\"; const char* app_charts_path = "c:\\CHARTS\\"; foo.h extern const char* app_data_path; extern const char* app_charts_path; making them static just means that every .CPP will get its own private copy of the strings. Image Toolkits | Image Processing | Cleek

        K 1 Reply Last reply
        0
        • C Chris Losinger

          kshaff03 wrote: Is this the best way to handled such a problem? nope. the best way is like this: foo.cpp: #include "foo.h" const char* app_data_path = "data\\"; const char* app_charts_path = "c:\\CHARTS\\"; foo.h extern const char* app_data_path; extern const char* app_charts_path; making them static just means that every .CPP will get its own private copy of the strings. Image Toolkits | Image Processing | Cleek

          K Offline
          K Offline
          K Shaffer
          wrote on last edited by
          #4

          Thanks Chris, As usual, your the best. -Kevin Kevin Shaffer kshaff03@msn.com

          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