【问题标题】:expected unqualified-id before string constant字符串常量之前的预期 unqualified-id
【发布时间】:2012-08-03 22:33:50
【问题描述】:

我目前正在编写一个 C++ 应用程序,它结合 math.h 实现振荡器。我拥有的代码应该适用于应用程序(尝试编译目标文件),但我遇到了一个编译器错误,很可能与语法/等有关;我认为这与命名空间有关。错误:

终端输出:

User-Name-Macbook-Pro:Synth Parts UserName$ make
g++ -o Oscillators.o -c -I. Oscillators.cpp -c
In file included from Oscillators.cpp:2:
/usr/include/math.h:41: error: expected unqualified-id before string constant
In file included from /usr/include/c++/4.2.1/bits/locale_facets.tcc:42,
             from /usr/include/c++/4.2.1/locale:46,
             from /usr/include/c++/4.2.1/bits/ostream.tcc:46,
             from /usr/include/c++/4.2.1/ostream:635,
             from /usr/include/c++/4.2.1/iostream:45,
             from Oscillators.cpp:4:
/usr/include/c++/4.2.1/typeinfo:41: error: expected declaration before end of line
make: *** [Oscillators.o] Error 1

振荡器.cpp

#include "Oscillators.h"
#include <math.h>
#include <vector>
#include <iostream>

#define TWOPI (6.2831853072)

using namespace std;

oscillator(int srate = 44100, int tabsize = 8192, double freq = 200) // Default to output 200Hz Sine Wave
{
    if(srate <= 0) {
        cout << "Error: sample rate must be positive" << endl;
        return;
    }
    sizeovrsr_ = (double)tabsize / (double)srate
    if(freq < 20 || freq > 20000) {
        cout << "Error: frequency is out of audible range" << endl;
        return;
    }
    curfreq_ = freq;
    curphase_ = 0.0 // Not out of one, out of tabsize
    incr_ = curfreq * sizeovrsr_;
    for(int i = 0; i < tabsize; i++) {
        gtable.push_back(sin((i*TWOPI)/(double)tabsize));
    }
    gtable.push_back(gtable[0]);
}

void print_table()
{
    vector<double>::size_type i;
    for(i = 0; i < gtable.size(); i++)
        cout << gtable[i] << "\n";
    cout << endl;
}

振荡器.h

#ifndef GUARD_OSCILLATORS_H
#define GUARD_OSCILLATORS_H
#include <vector>

class oscillator {
public:
    /*
    void fill_sine(); // Will change the table to a sine wave
    void fill_square(); // Will change the table to a square wave
    void fill_sawtooth(); // Will change the table to a sawtooth wave
    void fill_triangle(); // Will change the table to a triangle wave
    double tick(double freq); // Will output the current sample and update the phase
    */
    void print_table(); // Will print all table values to standard output
    oscillator(int srate = 44100, double freq = 200, int tabsize = 8192);
private:
    double sizeovrsr_; // Will be set at initialization and will determine phase increase for tick func
    double curphase_;
    double curfreq_; // if the freq sent to a tick function doesn't match the current tick, it will be reset;
    double incr_;
    std::vector<double> gtable_; // Will contain a wavetable with a guard point.
}
#endif

我已经看到其他提到使用 g++ -Wall -g 的建议,但我不确定这是问题所在,并且这里还有其他问题。

任何帮助将不胜感激!谢谢!!

【问题讨论】:

  • 您包含 两次,一次在标题中,一次在源文件中。
  • @pg1989 很好,标准头文件包含保护或等效项

标签: c++ namespaces g++ math.h object-files


【解决方案1】:

这是一个简单的问题。

您只是忘记了头文件末尾的分号。

由于在类定义末尾缺少分号而导致的编译器错误很难与实际问题联系起来 - 只要养成在创建类后检查错误时检查的习惯即可。

【讨论】:

    【解决方案2】:

    您的代码有多个问题:

    • 您需要在振荡器.cpp 中完全限定名称(void oscillators::print_table() 而不仅仅是 void print_table()
    • 您可能需要#include "oscillators.h" 进入振荡器.cpp
    • 您需要在实现中正确声明变量
    • 添加缺少的分号。

    但我猜特定错误是由于头文件中的类定义后缺少分号造成的。只需像这样添加它:

        std::vector<double> gtable_; // Will contain a wavetable with a guard point.
    };
    #endif
    

    【讨论】:

      【解决方案3】:

      产生此错误的另一种方法:将宏定义为字符串常量,然后将宏名称用作字符串常量的名称。示例

      #define FOO "bar"
      static const char FOO[] = "bar"; // <-- Error "expected unqualified-id before string constant".
      

      显而易见的答案是删除其中一个定义,或更改一个定义的名称。

      【讨论】:

        【解决方案4】:

        对我来说,它发生过一次,因为在较早的标头中声明了重复标识(同名)。

        【讨论】:

          猜你喜欢
          • 2023-03-07
          • 2017-12-23
          • 1970-01-01
          • 1970-01-01
          • 2015-02-15
          • 2014-09-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多