【发布时间】:2015-03-24 14:57:05
【问题描述】:
打印数组digitArray 和testo 的值显示不正确的值。
Display.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include "display.h"
#define D0 26
#define D1 27
#define D2 28
#define D3 29
#define BL 15
#define BM 16
#define BR 4
#define MI 5
#define TL 10
#define TM 11
#define TR 6
#define DOT 1
int segmentArray[8] = {BL, BM, BR, MI, TL, TM, TR, DOT};
int digitArray[4] = {D0, D1, D2, D3};
int testo[4] = {260, 1,2,3};
//Sets all segments on
void Display::test() {
std::cout << digitArray[0] << std::endl;
std::cout << D0 << std::endl;
std::cout << testo[3] << std::endl;
std::cout << segmentArray[0] << std::endl;
}
Display.h
class Display {
public:
void test();
private:
int SegmentArray[8];
int digitArray[4];
int testo[4];
};
main.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "display.h"
int main() {
Display display;
display.test();
return 0;
}
运行这段代码给了我:
35588 (incorrect, varies from time to time and appears to be some adress)
26 (correct)
0 (incorrect)
15 (correct)
【问题讨论】:
-
你发的sn-p没有问题。请发帖enough code 说明问题。
-
代码工作正常 - ideone.com/i3qDzI - 问题在别处。
-
刚刚添加了完整的代码。
-
calcSegments返回一个指向局部变量的指针。函数返回后,您的数组不会持续存在。要么返回std::array,要么使数组成为静态的。另外,不要将宏用于常量。 -
我要改变这些东西。但这部分确实有效。