C 类文件未在Linux OS上运行的Eclipse Mars中构建二进制文件

C++ Class Files Not building Binaries in Eclipse Mars Running on Linux OS

本文关键字:Eclipse Mars 二进制文件 构建 运行 文件 OS Linux      更新时间:2023-10-16

在执行我的代码时,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在为某些文件而不是其他文件构建项目时会生成二进制文件(例如,上面的文件包含一个称为MAUSE的类?)

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

顺便说一句,您的Startnote方法似乎有一个错误。看起来Key Triad()可以给出您的Switch语句不涵盖的值。那么在这种情况下如何构建您的对象?