“another_value”应该表示一个幻数还是另一个可变值?首先,您应该在定义所有基元时指定一个初始值。不要使用幻数,如果是这种情况,请使用#define ANOTHER_VALUE 0。
When I remove it from the C file and put it under the private variables in the header file, it compiles fine but then my output blows up.
可能是因为你需要为extern “C”,他们的变量,如果你是在protoype拉动使用从C代码:
How do I use extern to share variables between source files?
这里有一个编译例如:
externTest.hpp
/*
* externTest.hpp
*
* Created on: Mar 15, 2012
*/
#ifndef EXTERNTEST_HPP_
#define EXTERNTEST_HPP_
class eTest
{
private:
unsigned long m_test;
public:
eTest();
void testValue();
};
#endif /* EXTERNTEST_HPP_ */
externTest.cpp
//============================================================================
// Name : externTest.cpp
// Author :
// Version :
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "externTest.hpp"
extern "C" {
#include "value.h"
}
using namespace std;
eTest::eTest() {
m_test=global_table;
}
void eTest::testValue() {
printf("Global C Value: %lu \n", global_table);
cout << "Test global value: " << m_test << endl;
}
value.h
/*
* value.h
*
* Created on: Mar 15, 2012
*/
#ifndef VALUE_H_
#define VALUE_H_
extern unsigned long global_table;
void assign_value(unsigned long lValue);
void print_global_value();
#endif /* VALUE_H_ */
value.c
/*
* value.c
*
* Created on: Mar 15, 2012
*/
#include "value.h"
unsigned long global_table;
void assign_value(unsigned long lValue) {
global_table=lValue;
}
void print_global_value() {
printf("Global C Value: %lu \n", global_table);
}
如果你想使用表的物理地址,只是让m_test指针global_table:
private:
unsigned long *m_test;
请务必解除您的poi价值:
eTest::eTest() {
m_test=&global_table;
}
void eTest::testValue() {
printf("Global C Value: %lu \n", global_table);
cout << "Test global value: " << *m_test << endl;
}
请修正您使用的代码为例!使用该声明构造函数()是**不是**测试类的成员。这个例子根本不应该编译。 – 2012-03-15 17:53:08
为什么您将测试方法实施为全局方法?他们不能访问一个类的私有成员(我想知道它是如何编译的)。也许你的例子离真正的代码太远了。 – 2012-03-15 17:38:25
这不属于这里,因为你没有提出足够具体的问题来解答正确或错误的答案。 – Omnifarious 2012-03-15 17:57:57