我有以下情况。下面附有伪代码。我有一个 A 类,它有一个类型为 D 或 E 的对象 c,这会有所不同(实际上是随机决定的)。它使用 b 作为与远程计算机通信的消息。
代码:
class C
{
...
};
class D: public C
{
...
};
class E: public C
{
...
};
struct B
{
int a;
// If A->c is of type D
float b;
// If A->c is of type E
double b;
};
class A
{
B b;
C *c;
A()
{
c = …
Run Code Online (Sandbox Code Playgroud) 我编写了一个代码来读取文件,将其存储在一个结构中并显示它.但不知何故,它给了我一个分段错误,我不知道为什么.有人可以帮帮我吗?
输出:
file: /home/neel/map2.txt
file opened
Start Intersection
a->road: 4
a->roadId[0]: 1
a->lane[0][0]: 2
a->lane[0][1]: 2
a->roadId[1]: 2
a->lane[1][0]: 2
a->lane[1][1]: 2
a->roadId[2]: 3
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
码:
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
struct Intersection
{
unsigned short road;
long long int *roadId;
short *lane[2];
};
int main(int argc, char** argv)
{
std::ifstream file;
cout<<"file: "<<argv[1]<<endl;
file.open(argv[1], std::ios::in);
cout<<"file opened"<<endl;
while (!file.eof())
{
cout<<"Start Intersection"<<endl;
Intersection *a = new Intersection;
file>>a->road;
a->roadId = new long long int[a->road]; …
Run Code Online (Sandbox Code Playgroud) 我有一个代码,其中字符数组由整数填充(转换为char数组),并由另一个函数读取,该函数将其重新转换回整数.我使用以下函数来转换为char数组:
char data[64];
int a = 10;
std::string str = boost::lexical_cast<std::string>(a);
memcpy(data + 8*k,str.c_str(),sizeof(str.c_str())); //k varies from 0 to 7
Run Code Online (Sandbox Code Playgroud)
并使用以下方法重新转换回字符:
char temp[8];
memcpy(temp,data+8*k,8);
int a = atoi(temp);
Run Code Online (Sandbox Code Playgroud)
这一般工作正常,但是当我尝试将其作为涉及qt(ver 4.7)的项目的一部分时,它编译得很好并且在尝试使用memcpy()读取时给出了分段错误.请注意,分段故障仅在读取循环中发生,而不是在写入数据时发生.我不知道为什么会这样,但我希望通过任何方法完成它.
那么,还有其他任何我可以使用的函数可以接受字符数组,第一位和最后一位并将其转换为整数.然后我根本不必使用memcpy().我想要做的是这样的事情:
new_atoi(data,8*k,8*(k+1)); // k varies from 0 to 7
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我有以下类定义和main().有人可以指出我为什么会收到错误吗?
#include <iostream>
#include <list>
using namespace std;
class test
{
protected:
static list<int> a;
public:
test()
{
a.push_back(150);
}
static void send(int c)
{
if (c==1)
cout<<a.front()<<endl;
}
};
int main()
{
test c;
test::send(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:
/tmp/ccre4um4.o: In function `test::test()':
test_static.cpp:(.text._ZN4testC1Ev[test::test()]+0x1b): undefined reference to `test::a'
/tmp/ccre4um4.o: In function `test::send(int)':
test_static.cpp:(.text._ZN4test4sendEi[test::send(int)]+0x12): undefined reference to `test::a'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
即使我使用c.send(1)而不是test :: send(1),错误也是一样的.在此先感谢您的帮助.
我有两个模块
以下代码段最相关,并附在下面:
顶层模块:
计数器counter1(...,error_count); lcd lcd1(...,error_count);
计数器模块:
模块计数器(...,error_count); ... 输出寄存器[31:0] error_count = 0; ... //每个时钟周期更新计数器 终端模块
LCD模块:
模块LCD(...,error_count); ... 输入[31:0] error_count; ... //用于在LCD上显示的error_count 终端模块
此代码有什么问题?显示屏仅输出0作为输出。我传递向量的方式有什么问题吗?
附加信息:我正在使用Xilinx Spartan 3E入门套件来测试此代码。LCD代码很好,我已经使用本地计数器(reg [31:0])对其进行了测试。