Eclipse 方法无法在简单的程序 C++ 中解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9124882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:19:41  来源:igfitidea点击:

Eclipse Method could not be resolved in a simple program C++

c++eclipseclassmethods

提问by F_C

I have an issue with Eclipse Indigo complaining that methods of a class couldn't be resolved, but compiling anyway and working correctly (AFAIK). It's a very simple program. Here is Population.cpp:

我有一个 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";
}

Now, I build that code and everything is fine. In another project within Eclipse, I'm calling this code like this:

现在,我构建了该代码,一切都很好。在 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);
}

As you can see I'm also running the CUTE plugin for TDD in C++. It doesn't complain when I declare p as type Population and the first assertion passes. I'm somewhat new to C++, but I did make sure to add the project that Population.cpp is from to the include path for the test project.

如您所见,我还在 C++ 中运行 TDD 的 CUTE 插件。当我将 p 声明为 Population 类型并且第一个断言通过时,它不会抱怨。我对 C++ 有点陌生,但我确实确保将 Population.cpp 来自的项目添加到测试项目的包含路径中。

It's not a huge deal as it's not affecting anything obvious to me, but it's still very annoying. I don't see a situation where it should do this.

这不是什么大问题,因为它不会影响任何对我来说显而易见的事情,但它仍然很烦人。我没有看到应该这样做的情况。

Thanks for any help!

谢谢你的帮助!

回答by user1526533

Try this:

尝试这个:

In your project explorer window, right click on your project -> Index -> Rebuild

在您的项目资源管理器窗口中,右键单击您的项目 -> 索引 -> 重建

回答by iammilind

This could be an indexing issue related to external #includeheaders not found. Follow the steps below and see if it helps:

这可能是与#include未找到外部标头相关的索引问题。请按照以下步骤操作,看看是否有帮助:

  1. Go to each of your custom #include(e.g. "cute.h") and press F3(i.e. "Show declaration"); see if it's able to access that file or not; if not copy those files on some notepad
  2. If the file is not accessible, then locate it paths in your directory structure; e.g. "cute.h" and "a.h" are located at, "C://Eclipse/MyWork/Workspace/Project/include_1" and "ide_listener.h" is located at,"C://Eclipse/MyWork/Workspace/Project/include_2", then copy both the folder paths on some notepad
  3. Inside Eclipse go to Project -> Properties -> C/C++ General -> Paths and Sybmols; you will see several tabs as Includes, Sybmols, Library Paths...
  4. Click Library Paths -> Add -> Workspace... -> <locate the above folder paths>and press OK
  5. Let the indexer rebuild; now again follow the step (1); hopefully the files should be accessible
  6. For future safety for larger files, go to Window -> Preferences -> C/C++ -> Editor -> Scalability -> "Enable scalability mode when ..."and set the number of lines to some big number such as 500000and press "OK";
  1. 转到您的每个自定义#include(例如"cute.h")并按 F3(即“显示声明”);看看它是否能够访问该文件;如果不将这些文件复制到某个记事本上
  2. 如果该文件不可访问,则在您的目录结构中找到它的路径;例如,“cute.h”和“ah”位于“ C://Eclipse/MyWork/Workspace/Project/include_1”,而“ide_listener.h”位于“ C://Eclipse/MyWork/Workspace/Project/include_2”,然后将两个文件夹路径复制到某个记事本上
  3. 进入 Eclipse 内部Project -> Properties -> C/C++ General -> Paths and Sybmols;您会看到几个选项卡,如Includes, Sybmols, Library Paths...
  4. 单击Library Paths -> Add -> Workspace... -> <locate the above folder paths>并按确定
  5. 让索引器重建;现在再次按照步骤(1)进行操作;希望文件应该可以访问
  6. 为了将来更大文件的安全,请转到Window -> Preferences -> C/C++ -> Editor -> Scalability -> "Enable scalability mode when ..."并将行数设置为一些大数字,例如 500000并按“确定”;

The last step is needed, because when your file grows in line number and if exceeds the above number then eclipse will stop showing definitions for some "scalability" reasons, even though it would have indexed.

最后一步是必需的,因为当您的文件在行号中增长并且如果超过上述数字时,eclipse 将由于某些“可扩展性”原因停止显示定义,即使它会被索引。

回答by Some programmer dude

sizeof(pointer)returns the size of the pointer (4 on 32-bit systems and 8 on 64-bit), not the size of what it points to! Save the dimensions in the class, and add a function to return them.

sizeof(pointer)返回指针的大小(在 32 位系统上为 4,在 64 位系统上为 8),而不是它指向的大小!将维度保存在类中,并添加一个函数来返回它们。

Also, in initializePopshouldn't you allocate the actual Xarray?

另外,initializePop您不应该分配实际的X数组吗?

X = calloc(N, sizeof(char *));

Or rather, you should use newfor allocation since you are using C++:

或者更确切地说,您应该使用newfor allocation,因为您使用的是 C++:

X = new char* [N];

and later:

然后:

X[i] = new char [numbits];

回答by Stormshadow

Not that i'm any expert at all but i just had a similar problem using.empty for whatever reason it will work if you change char to string minor change but resolved the issue on my program

并不是说我是任何专家,但我只是在 using.empty 中遇到了类似的问题,无论出于何种原因,如果您将 char 更改为 string 小改但解决了我程序上的问题,它会起作用