相关疑难解决方法(0)

C++在哪里初始化静态const

我上课了

class foo {
public:
   foo();
   foo( int );
private:
   static const string s;
};
Run Code Online (Sandbox Code Playgroud)

s源文件中初始化字符串的最佳位置在哪里?

c++ static initialization constants

122
推荐指数
4
解决办法
15万
查看次数

编译和工作的静态const初始化错误

据我所知,如果静态const成员是整数类型,则只能在它们的声明的同一行初始化.但是,我仍然能够初始化并使用一些静态const双精度:

// compiles and works, values are indeed doubles
struct Foo1{ 
    static const double A=2.5;
    static const double B=3.2;
    static const double C=1.7;
};

// compiles, but values are cast to int
struct Foo2{
    static const int A=2;
    static const int B=3;
    static const double C=B/A; //becomes 1
};

// does not compile, Foo3::B cannot appear in a constant-expression
struct Foo3{ 
    static const int A=2;
    static const double B=3;
    static const double C=A/B; 
};

// does not compile, …
Run Code Online (Sandbox Code Playgroud)

c++ gcc compiler-errors

5
推荐指数
1
解决办法
711
查看次数

C++内联初始化静态const char*

为什么我不能在头文件中初始化静态const char*?在我的代码中,我在我的类头中:

static const char* xml_ID_TAG;
Run Code Online (Sandbox Code Playgroud)

在cpp中:

const char* Class::xml_ID_TAG = "id";
Run Code Online (Sandbox Code Playgroud)

xml_ID_TAG变量包含XML文档的属性字符串.因为它是静态的,const,原始类型(char*)等...我无法弄清楚为什么编译器禁止编写类似的东西:

static const char* xml_ID_TAG = "id";
Run Code Online (Sandbox Code Playgroud)

我正在使用MSVC2013编译器,给出上面的错误示例:"错误:具有类内初始化程序的成员必须是const"

c++

4
推荐指数
2
解决办法
1万
查看次数

没有构造函数的类中的C++非静态const成员

我一直收到这个错误:

警告:没有构造函数的类中的非静态const成员'const char sict :: Weather :: _ date [7]'[-Wuninitialized]

我不明白.

main.cpp中

#include <iostream>
#include <iomanip>
#include "Weather.h"
using namespace std;
namespace sict{

int main(){
    int n; 
    Weather* weather;

    cout << "Weather Data\n";
    cout << "=====================" << endl;
    cout << "Days of Weather: ";
    cin >> n;
    cin.ignore();
    weather = new Weather(n);

    for (int i = 0; i < n; i++){
        char date_description[7];
        double high = 0.0, low = 0.0;

        cout << "Enter date: ";
        cin >> date_description;
        cout << …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
2314
查看次数

关于工作C++ 03代码的G ++(C++ 14)链接器错误

考虑以下代码.

class aClass
{
public:
    static const int HALLO = -3;
};

int main()
{
  std::vector<double > a;
  std::vector<int> b;
  std::vector<int> c;
  int d = aClass::HALLO; //fine
  a.resize(10,aClass::HALLO); //fine
  b.resize(10,aClass::HALLO); // linker error c++11 and c++14
  c.resize(10,(int)(double)aClass::HALLO); //fine
  std::cout<<a[0]<<endl;
  std::cout<<b[0]<<endl;
  std::cout<<c[0]<<endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译与C++ 03一起使用并产生输出:

-3
-3
-3
Run Code Online (Sandbox Code Playgroud)

但是,使用C++ 11或C++ 14进行编译会导致链接器错误:

/tmp/cc3BARzY.o: In Funktion `main':
main.cpp:(.text+0x66): Nicht definierter Verweis auf `aClass::HALLO'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

奇怪的是,这只发生在矢量上b.如果有一个强制转换为double(a)或甚至是double并返回到int(c),则代码按预期运行.

如何解释这种行为?

c++ static g++ linker-errors

2
推荐指数
1
解决办法
157
查看次数