相关疑难解决方法(0)

如何在C++中转发声明模板类并将其用作成员数据?

我试图转发声明一个模板类,然后使用该类在其他类中声明成员数据。代码如下:

\n\n
using namespace std;\n\ntemplate<class T>\nclass B;\n\nclass A{\n   B<T> b;\n};\n\ntemplate<class T>\nclass B{\n    T x;\n};\n\nint main(){\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我得到编译错误:

\n\n
error: \xe2\x80\x98T\xe2\x80\x99 was not declared in this scope\n     B<T> b;\n
Run Code Online (Sandbox Code Playgroud)\n\n

有人可以让我知道我做错了什么以及如何实现我的目标吗?(我注意到关于模板类的帖子,但没有一个回答我的问题。)

\n\n

预先非常感谢!

\n

c++

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

为什么允许具有本身具有不完整类型静态成员的类?

我很难理解为什么类可以具有自身的不完整类型静态成员。

例如在下面的代码中。为什么允许在类A内拥有自身的不完整类型静态成员,但是当类类型B的全局静态变量是不完整类型时却出现错误?

class B {
public:
    B(int a) {
    }
};

static B test2; //error


class A {

public:
    static A test; //accepted

    A(int d) {
    }
};
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释在后台给全局静态变量带来错误的原因是什么,为什么A类内部的静态变量会被接受?

c++

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

c ++函数指针声明导致程序在退出时崩溃

我在我的类中声明了以下两个函数指针:

void (*ptrFunc)(void *);
bool (*ptrValid)(char *);
Run Code Online (Sandbox Code Playgroud)

现在由于某种原因,第二个指针(ptrValid)导致程序在退出时崩溃.当我发表声明时,程序退出正常,但当我取消评论时,它会崩溃.

没有任何东西被赋予它,它没有被召唤,只是被宣布.

我在这里错过了什么吗?

c++ function-pointers function

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

我的C++类无法链接到另一个.cpp文件中的函数

我昨天问了同样的问题,答案不适用.

/sf/ask/433620491/

如果我进入类标题,右键单击该函数并单击"转到定义",这使我在我的其他.CPP文件中使用了我的函数.它看到它,可以链接到它,但我仍然得到错误,表明我看不到它.

有人有什么建议吗?我会尝试任何事情.

这是错误.

zdll.lib(d000050.o):警告LNK4078:'.text'找到具有不同属性的多个部分(E0300020)

WLD.obj:错误LNK2019:"public: void __thiscall WLD::fragment_03(unsigned char *,int)" (?fragment_03@WLD@@QAEXPAEH@Z)函数中引用的未解析的外部符号"public: bool __thiscall WLD::init(unsigned char *)" (?init@WLD@@QAE_NPAE@Z)

编辑:另外,我正在使用MSVC++.我应该尝试创建新解决方案并导入文件吗?可能会有所帮助,因为我觉得我没有选择......

编辑:这是代码:

#include "WLD.h"

inline void WLD::fragment_03(uchar* location, int frag_num)
{
    // Read the struct into memory and create a temporary pointer
    struct_frag03 temp03;
    memcpy(&temp03, location, sizeof(struct_frag03));
    uchar* temp_p = location;

    // Advance the pointer to the encoded bytes (filename)
    temp_p += sizeof(long) + sizeof(short);

    // Grab the encoded filename and decode it
    uchar* …
Run Code Online (Sandbox Code Playgroud)

c++ linker class

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

在自己的.cpp中定义一个结构,然后在main中使用它

所以我尝试在自己的.cpp文件中定义一个结构(用于组织或其他),我转发在头文件中声明它,包含在.cpp文件中的头文件,然后在main.cpp中包含头文件并尝试创建一个结构的向量,它没有用.然而,我接受了struct的定义并将其放入main.cpp,它确实有效.这只是我不知道的结构的怪癖,它们需要在文件中定义它们被使用(出于某种原因)?这是代码:

//people.h
struct People;

//people.cpp
#include people.h
struct People
{
std::string name;
int age;
};

//main.cpp
#include"people.h"
#include<vector>
std::vector<People> list;
Run Code Online (Sandbox Code Playgroud)

c++

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

C预处理器防护?

我正在研究一个C项目,我正在尝试使用可以在C++中使用的预处理器防护:

#ifndef CONFIG_H
#define CONFIG_H

... exciting stuff in C ....

#endif
Run Code Online (Sandbox Code Playgroud)

在我的源代码中包含这个似乎对Visual Studio没有影响,因为当我包含给定文件(例如Config.h,在多个文件中)时,编译器会给我以下错误:

1>main.obj : error LNK2005: _OPCodes already defined in lib.obj
1>main.obj : error LNK2005: _OPTotal already defined in lib.obj
1>main.obj : error LNK2005: _RegCodes already defined in lib.obj
1>main.obj : error LNK2005: _RegTotal already defined in lib.obj
1>main.obj : error LNK2005: _UDSTotal already defined in lib.obj
Run Code Online (Sandbox Code Playgroud)

有人可以给我任何指针(没有双关语)吗?

c c++ guard c-preprocessor

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

C++ 中的函数位置重要吗?

#include <iostream>\nusing namespace std;\nint my_variable = 12;\nvoid cappy()\n{\n    std::cout<<"value is"<< my_variable<<endl;\n}\n\nint main()\n{\nstd::cout<< my_variable<<endl;\ncappy();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

此 C++ 代码工作并返回:

\n\n
12\nvalue is12\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是:

\n\n
#include <iostream>\nusing namespace std;\nint my_variable = 12;\n\nint main()\n{\nstd::cout<< my_variable<<endl;\ncappy();\n}\n\nvoid cappy()\n{\n    std::cout<<"value is"<< my_variable<<endl;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这段代码返回一个错误:

\n\n
cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\ncpp:8:7: error: \xe2\x80\x98cappy\xe2\x80\x99 was not declared in this scope\n
Run Code Online (Sandbox Code Playgroud)\n\n

为什么会这样呢?C++ 中函数的位置重要吗?

\n

c++

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

为什么这会给出多个定义错误:extern int i; int i;

如果有多个声明可能并且只允许一个定义(ref),那么为什么下面的代码会i在Turbo C++编译器中给出一个错误作为多个声明?

int main()
{
    extern int i;
    int i;
    printf("The value of i is %d",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c c++

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

花括号对于空构造函数是否重要?

我想知道以下构造函数是否与C++相同:

class foo
{ 
public:
   foo(void){};
...
}
Run Code Online (Sandbox Code Playgroud)

class foo
{
public:
   foo(void);
...
}
Run Code Online (Sandbox Code Playgroud)

大括号对这两种情况有影响吗?非常感谢!

c++

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

编译 C++ 时未定义的引用

我的代码与此类似,但问题完全相同:在 VSCode 中编译程序时,我在 Test1.cpp 和 Test2.cpp 中得到“对`Test1::v 的未定义引用”。我究竟做错了什么?我对 C++ 有点陌生,所以我刚刚下载了一个扩展,它使我自动成为 C++ 项目。当我使用 Ctrl + Shift + B 运行程序时,它给了我这个错误,但是当我使用 Code Runner 扩展名时,它没有检测到 .cpp 文件。

// Test1.h
#include <iostream>
#include <vector>

using namespace std;

#ifndef TEST1_H
#define TEST1_H

class Test1{
    public:
        Test1();
        static vector<Test1> v;
        int a;

};

#endif
Run Code Online (Sandbox Code Playgroud)
//Test1.cpp
#include "Test1.h"

Test1::Test1(){
    a = 2;
    v.push_back(*this);
}
Run Code Online (Sandbox Code Playgroud)
//Test2.h
#include <iostream>
#include <vector>

using namespace std;

#ifndef TEST2_H
#define TEST2_H

class Test2{
    public:
        Test2();
        double var;
};

#endif
Run Code Online (Sandbox Code Playgroud)
//Test2.cpp
#include "Test2.h"
#include …
Run Code Online (Sandbox Code Playgroud)

c++ include header-files visual-studio-code

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