How do I define string constants in C++? [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ static constant string (class member)
static const C++ class member initialized gives a duplicate symbol error when linking
My experience with C++ pre-dated the addition of the string class, so I'm starting over in some ways.
I'm defining my header file for my class and want to create a static constant for a url. I'm attempting this by doing as follows:
#include <string>
class MainController{
private:
static const std::string SOME_URL;
}
const std::string MainController::SOME_URL = "www.google.com";
But this give me a duplicate definition during link.
How can I accomplish this?

Move the
const std::string MainController::SOME_URL = "www.google.com";
to a cpp file. If you have it in a header, then every .cpp that includes it will have a copy and you will get the duplicate symbol error during the link.

You need to put the line
const std::string MainController::SOME_URL = "www.google.com";
in the cpp file, not the header, because of the one-definition rule. And the fact that you cannot directly initialize it in the class is because std::string is not an integral type (like int).
Alternatively, depending on your use case, you might consider not making a static member but using an anonymous namespace instead. See this post for pro/cons.

Define the class in the header file:
//file.h
class MainController{
private:
static const std::string SOME_URL;
}
And then, in source file:
//file.cpp
#include "file.h"
const std::string MainController::SOME_URL = "www.google.com";

You should put the const std::string MainController::SOME_URL = "www.google.com"; definition into a single source file, not in the header.

Related

Using the const keyword in header and class files functions C++ [duplicate]

This question already has answers here:
When to use const and const reference in function args?
(4 answers)
C++ Const Usage Explanation
(12 answers)
Closed 4 years ago.
I am trying to learn how to use the keyword const while making header and class files (using OOP). Aka learning the correct way to incorporate the keyword 'const' while making and calling the functions.
// Example.h
class Example {
public:
string getName() const;
void setName(const string aName);
private:
const string name;
};
// Example.cpp
#include "Example.h"
#include <string>;
#include <iostream>;
Example::Example();
string Example::getName() const{
return name;
// the following setter does not work
void Example::setName(const string aName){
name = aName;
}
I figured out how to declare variable and getter/setter functions, using const in the header file. Just need help in using const with setter function in class file.
// the following setter does not work
void Example::setName(const string aName){
name = aName;"
Of course it doesn't. You declared name to be const so you cannot assign to it (you can only initialize it). Remove const from name and your setter will work.

How to use info passed as parameter to build a string defined in other CPP file

I have a C++ program that has several .hpp files with declarations of variables (most of them paths to a NFS filesystem) and .cpp files with the definitions of those variables.
In some of those variables, whose type is std::string, I need to build its content by appending some content passed as parameter in the main program. For instance:
File constants.hpp:
namespace constants {
extern std::string cudnn_version;
extern const std::string path_caffe_cuda;
extern const std::string path_caffe_cuda_cudnn;
}
File constants.cpp:
const std::string constants::path_caffe_cuda = "/nfs/apps/caffe/cuda";
const std::string constants::path_caffe_cuda_cudnn = constants::path_caffe_cuda + "/cudnn" + constants::cudnn_version;
The content of constants::cudnn_version is asked to the user in the main program as parameter and updated there. The problem is, when constants::path_caffe_cuda_cudnn variable must be built with the content of constants::cudnn_version variable, its content is still empty, so in some way the variable path_caffe_cuda_cudnn is evaluated before the constants::cudnn_version has the content passed by user.
How do you think I could fix the issue?
Thank you very much to everybody.
It could easily be done by using a function instead:
namespace constants {
extern std::string cudnn_version;
extern const std::string path_caffe_cuda;
inline std::string path_caffe_cuda_cudnn()
{
return constants::path_caffe_cuda + "/cudnn" + constants::cudnn_version;
}
}
As long as path_caffe_cuda_cudnn is not called until constants::cudnn_version have been initialized, then it will be okay.

Putting all const values of a class in the same place

I am trying to use some compile-time const values for a class and I am using constexpr/const, but I dislike a lot the fact that some will be inited in the hpp and some in the .cpp.
I've read the explanation related to string incompatibility with constexpr, but
isn't a trick to put all of them in the same location?
It's strange that in the modern C++ 11/14 you have to declare them in two separate locations.:)
Thank you
// test.hpp
class Test
{
// initialization values are located clear in the header
static constexpr int a_{10};
static constexpr int b_{20};
// we must go to the cpp file to see this string value
static const std::string str_;
};
// test.cpp
const std::string Test::str_{"abc"};

How to reference static member variables from a struct within another class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 8 years ago.
I'm trying to define some static constant strings in C++ and reference them from different files.
Here's how I have the information set up this far:
structClass.h
namespace test {
typedef struct customstructure{
static const std::string stringA;
} customstructure;
}
structClass.cpp
namespace test {
static const std::string customstructure::stringA = "This is String A";
}
Now I'm wondering how I would call this in a third file?
execute.cpp
void printStringA(){
printf("stringA is: %s", test::customstructure::stringA.c_str());
}
gives me a compile error that says:
undefined reference to 'test::customstructure::stringA'
In this code:
namespace test {
static const std::string customstructure::stringA = "This is String A";
}
remove the word static. In fact it is an error to have this, your compiler should give a more useful error message (although I suppose 'undefined reference' meets the requirements of "a diagnostic").
Standard reference: [class.static.data]#5 says that static data members have external linkage, however using the keyword static in the definition would specify internal linkage.

c++ static function unfound

I have this definition of the function in my class.
The .hpp file:
class SomeClass
{
public:
static string DoStuff(string s);
};
The .cpp file:
#include "header.hpp"
string SomeClass::DoStuff(string s)
{
// do something
}
Compiler says:
**error C2039: 'DoStuff' : is not a member of 'SomeClass'**
Can somebody help?
EDIT:
actual offending code
header definition
class DDateTime{
public:
static string date2OracleDate(DATE Date);
}
string DDateTime::date2OracleDate(DATE Date)
{
string s;
s="TO_DATE('" + DDateTime::DateFormat("%d/%m/%Y",Date) + "','dd/MM/YYYY')";
return s;
}
Usually, .cpp files must include the matching .h or .hpp file.
Is it the case here ?
You can also have namespace issue (missing namespace in .cpp file or static method definition outside of the namespace, and so on.).
Actually, it is difficult to answer until we have the real breaking code.
Moreover, I don't know if this is sample code, but it seems you used something like using std::string or using namespace std in your header file.
This is a bad idea because it will polute every file in which your header is included. What If someone wants to use your header file but don't want to "use" std because string is the name of one of its classes ?
Have you included the header file in your cpp file?
Maybe a namespace issue? You could have a SomeNamespace::SomeClass with a static member function and a ::SomeClass in the outer namespace without the static member function.
Are you missing
#include<string>
in your header file?
Are you trying to call DoStuff from a double pointer to your instance? Example:
SomeClass **class;
class->DoStuff();
If so do this:
SomeClass **class;
(*class)->DoStuf();