【问题标题】:C++ Class Files Not building Binaries in Eclipse Mars Running on Linux OSC++ 类文件未在 Linux OS 上运行的 Eclipse Mars 中构建二进制文件
【发布时间】:2016-06-20 18:21:35
【问题描述】:

C++ 新手,在执行我的代码时遇到可怕的“找不到二进制文件”错误。这只发生在我创建了一个包含类代码的源文件时。当我编写不包含类的程序时,不会发生这种情况。例如,当我为它构建项目并运行良好时,该程序会创建二进制文件:

#include <iostream>
using namespace std;

int main()
{
  cout << "Hello World!";
}

但是当我尝试为这个文件构建一个项目时,二进制文件不会被创建:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <algorithm>    // std::max

using namespace std;

class Measure {
public:
int key;
string note;

Measure() {
    key = 13;
    //Generate a random key
    while(key > 12)
    {
        key = (rand() % 100) + 1;
    }
    note = startNote(key);
}

int getKey() {
    return key;
}

string getNote() {
        return note;
    }

int triad(){
    int first = 0;
    int third = 0;
    int fifth = 0;
    while(first == third && first == fifth )
    {
        first = (rand() % 100) + 1;
        third = (rand() % 100) + 1;
        fifth = (rand() % 100) + 1;
    }
    if (first > third && first > fifth)
        return 0;
    else if(third  > first && third > fifth)
        return 1;
    else
        return 2;
}

string startNote(int key){
    switch(key + triad())
    {
    case 1 :
        note = "A";
        break;
    case 2 :
        note = "A#";
        break;
    case 3 :
        note = "B";
        break;
    case 4 :
        note = "C";
        break;
    case 5 :
        note = "C#";
        break;
    case 6 :
        note = "D";
        break;
    case 7 :
        note = "D#";
        break;
    case 8 :
        note = "E";
        break;
    case 9 :
        note = "F";
        break;
    case 10 :
        note = "F#";
        break;
    case 11 :
        note = "G";
        break;
    case 12 :
        note = "G#";
        break;
    }
    return note;
}



~Measure() {
    // TODO Auto-generated destructor stub
}

int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

};

我查找了与我类似的问题,但解决方案不起作用。我尝试将项目属性菜单中的二进制解析器更改为 PE Windows Parser、Elf Parser 和 GNU Elf Parser。这似乎没有什么不同。为什么 Eclipse 在为某些文件而不是其他文件构建项目时会生成二进制文件(例如,我上面的文件包含一个名为 Measure 的类?)

【问题讨论】:

    标签: c++ linux eclipse binaries


    【解决方案1】:

    您的主要功能在您的类定义中。您需要移动 };在您的主要功能之上。我不知道eclipse,但它可能在这里看不到main函数,只有一个名为main()的类方法,这在C++中是没有的。

    顺便说一句,您的 startNote 方法似乎有一个错误。看起来 key + triad() 可以提供 switch 语句未涵盖的值。那么在这种情况下你的对象是如何构造的呢?

    【讨论】:

    • 嘿,谢谢!那行得通(很抱歉犯了这样一个新手错误,顺便说一句。)而且,很好地抓住了 switch 语句。我认为实际上有两个我没有考虑的额外案例。我给他们分配了 A 和 A#。稍后我将不得不找出一种表示八度音阶的方法。 :) 再次感谢吉姆!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2016-06-05
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多