相关疑难解决方法(0)

无法访问类的成员

我有一点问题,我可能错误地包含了类文件,因为我无法访问敌人类的成员.我究竟做错了什么?我的班级cpp

#include "classes.h"

class Enemy
{
 bool alive;
 double posX,posY;
 int enemyNum;
 int animframe;
public:
Enemy(int col,int row)
{
    animframe = rand() % 2;
    posX = col*50;
    posY = row*50;
}

Enemy()
{

}
void destroy()
{
    alive = 0;
}
void setposX(double x)
{x = posX;}
void setposY(double y)
{y = posY;}
};
Run Code Online (Sandbox Code Playgroud)

我的标题:

class Enemy;
Run Code Online (Sandbox Code Playgroud)

我的主要:

#include "classes.h"
Enemy alien;
int main()
{
    alien. // this is where intelisense tells me there are no members
}
Run Code Online (Sandbox Code Playgroud)

c++ class

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

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

在静态类中初始化静态指针

所以我有以下c ++类

class MyClass:

public: 

    static void InitObject();

private:

    static MyObject *myObject;       

};
Run Code Online (Sandbox Code Playgroud)

然后在.cpp文件中我做

void MyClass::InitObject
{
    myObject = new MyObject();
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到编译器错误,说"myObject"是从InitObject()引用的,然后它说Linker命令失败,退出代码为1.

为什么这不起作用?我如何解决它?

c++ linker static pointers class

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

C++ 内联初始化静态函数成员

我想实现一个成员函数,如下所示:

void X() {}

class Foo
{
    static void(*Bar)() = X;
};
Run Code Online (Sandbox Code Playgroud)

这不会编译:

错误:非整数类型的静态数据成员“void (* Foo::Bar)()”的类内初始化需要“constexpr”

我知道这是不合法的。我必须在类范围之外初始化 Bar 或将其设置为“内联静态”。问题是后者是 C++17 功能,而我必须使用 C++11(BCC32X 限制)。所以我的问题是:有没有办法在同一行上做到这一点?也许使它成为常量?我知道我们可以做到这一点(来源)...

class Foo
{
    static int const i = 42;
}
Run Code Online (Sandbox Code Playgroud)

但我们能以某种方式将它应用到函数中吗?

PD:我知道我的问题有无限的解决方案,但到目前为止,我所看到的所有解决方案最终都依赖于我无法使用的后来的 C++ 功能。

c++ inline class function c++11

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

是否需要单独定义C++中的类或结构的常量静态成员变量?

是否需要单独定义C++中的类或结构的常量静态成员变量?

它是否正确?

struct test
{
    const static int x;
};

int test::x;
Run Code Online (Sandbox Code Playgroud)

c++ static const

0
推荐指数
1
解决办法
228
查看次数

初始化私有静态变量c ++

我想创建一个只包含一个sql :: Connection指针的Connector类.我的尝试是构建一个单例类,并使指针和构造函数本身私有.只有一个静态函数是公共的,允许创建连接.

我尝试使用

Connector::conn = 0;
Run Code Online (Sandbox Code Playgroud)

在实现模块中失败,因为conn是私有的,不能从外部访问.

如果我省略initiliziation我得到一个未定义的引用错误

Connector.h

#ifndef CONNECTOR_H
#define CONNECTOR_H

#include <cppconn/driver.h>
#include <cppconn/exception.h>

class Connector {

public:
    static sql::Connection* getConnection();


protected:
    Connector();
    Connector(const Connector& other) { }

    static sql::Connection * conn;
    static sql::Driver * drive;
};

#endif  /* CONNECTOR_H */
Run Code Online (Sandbox Code Playgroud)

Connector.cpp

#include "Connector.h"

Connector::Connector() {
}

sql::Connection * Connector::getConnection() {
    if(Connector::conn == 0) {
        Connector::drive = get_driver_instance();
        Connector::conn = Connector::drive->connect("tcp://any.de:3306", "any", "any");
        Connector::conn->setSchema("cryptoTool");
    }
    return Connector::conn;
}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors initialization declaration

0
推荐指数
1
解决办法
183
查看次数

为什么我们不能在构造函数初始化列表中初始化静态变量,但我们可以在构造函数体中

我总是读取初始化列表比构造函数体更适合初始化.我也知道静态变量可以在类外部进行初始化.

但我的问题是为什么我们不能在构造函数初始化列表中初始化静态变量,但我们可以在构造函数体中

class sample
{
    static int i;
public:
    sample (int ii=20) { i=ii;}
    void show()
    {
        cout << i << endl;
    }

};

int sample::i=12;

int main()
{
    sample s;
    s.show();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

工作正常并打印20.但我替换构造函数

sample (int ii=20): i(ii){}
Run Code Online (Sandbox Code Playgroud)

它给出了错误.为什么?

c++ constructor

0
推荐指数
2
解决办法
1189
查看次数

静态类中的向量

我想在静态类中有一个静态(常量)向量。向量不会填充新元素,它只会包含在 class configuration: 中定义的元素{1,2,3,4,5}

我怎样才能使向量debugLevels静态?

#include <ArduinoSTL.h>

class configuration {
  public:
  static std::vector<int> const debugLevels = {1,2,3,4,5}; // throws error: in-class initialization of static data member 'const std::vector<int> configuration::debugLevels' of incomplete type
};

void setup() {
  for(int i=0; i<configuration::debugLevels.size(); i++ {
    // do some stuff here...
  }
}

void loop() {
}
Run Code Online (Sandbox Code Playgroud)

c++ arduino

0
推荐指数
1
解决办法
75
查看次数