F_C*_*F_C 7 c++ eclipse methods class
我有一个问题,Eclipse Indigo抱怨类的方法无法解决,但无论如何编译并正常工作(AFAIK).这是一个非常简单的程序.这是Population.cpp:
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include "Population.h"
Population::Population() {
// TODO Auto-generated constructor stub
}
Population::~Population() {
// TODO Auto-generated destructor stub
}
void Population::initializePop(int numBits, int N) {
srand((unsigned)time(0));
for(int i=0; i<N; i++) {
x[i] = (char*) calloc(numBits, sizeof(char));
for(int j=0; j<numBits; j++) {
if( rand() < 0.5 )
x[i][j] = 0;
else
x[i][j] = 1;
}
}
}
char** Population::getX() {
return x;
}
void Population::printStuff() {
std::cout << "Whatever";
}
Run Code Online (Sandbox Code Playgroud)
现在,我构建了代码,一切都很好.在Eclipse中的另一个项目中,我正在调用这样的代码:
#include <typeinfo>
#include <string.h>
#include <iostream>
#include "cute.h"
#include "ide_listener.h"
#include "cute_runner.h"
#include "Population.cpp"
void testPopulationGeneration() {
Population* p = new Population;
int N = 10;
int bits = 4;
char** pop;
ASSERTM("Population variable improperly initialized", dynamic_cast<Population*>(p));
std::cout << p->printStuff();
std::cout << "Ok...";
p->initializePop(bits, N);
pop = p->getX();
ASSERTM("Pop not correct size.", sizeof(pop) == 10);
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我还在C++中运行TDD的CUTE插件.当我将p声明为类型Population并且第一个断言通过时,它不会抱怨.我对C++有些新意,但我确实确保将Population.cpp所在的项目添加到测试项目的包含路径中.
这并不是什么大不了的事,因为它对我来说没有任何明显的影响,但它仍然非常烦人.我没有看到应该这样做的情况.
谢谢你的帮助!
iam*_*ind 10
这可能是与#include未找到的外部标头相关的索引问题.请按照以下步骤查看是否有帮助:
#include(例如"cute.h")和按
F3(即"显示声明"); 看看它是否能够访问该文件; 如果不在某些记事本上复制这些文件C://Eclipse/MyWork/Workspace/Project/include_1"和"ide_listener.h"位于",C://Eclipse/MyWork/Workspace/Project/include_2",然后在记事本的一些拷贝这两个文件夹路径Project -> Properties -> C/C++ General ->
Paths and Sybmols; 你会看到几个选项卡为Includes,
Sybmols,Library Paths...Library Paths -> Add -> Workspace... -> <locate the above
folder paths>并按"确定"Window -> Preferences ->
C/C++ -> Editor -> Scalability -> "Enable scalability mode when
..."并将行数设置为某个大数字,
500000然后按"确定";需要最后一步,因为当你的文件增长的行数,如果超过上述数量,然后Eclipse将停止显示一些"定义的可扩展性 "的原因,即使它会索引.
sizeof(pointer)返回指针的大小(32 位系统上为 4,64 位系统上为 8),而不是它所指向的大小!将尺寸保存在类中,并添加一个函数来返回它们。
另外,initializePop您不应该分配实际的X数组吗?
X = calloc(N, sizeof(char *));
Run Code Online (Sandbox Code Playgroud)
或者更确切地说,您应该使用newfor 分配,因为您使用的是 C++:
X = new char* [N];
Run Code Online (Sandbox Code Playgroud)
然后:
X[i] = new char [numbits];
Run Code Online (Sandbox Code Playgroud)