「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