在字符串常量之前出现了未预期的未标识符。

38

我目前正在编写一个使用math.h实现振荡器的C++应用程序。我认为我的代码应该可以正常工作(尝试编译对象文件),但是我遇到了一个编译器错误,很可能与语法等有关;我认为这与命名空间有关。错误信息:

终端输出:

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

Oscillators.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;
}

Oscillators.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,但我不确定那是问题所在,而且可能还有其他问题。

非常感谢任何帮助!谢谢!!


你在头文件和源文件中都包含了 <vector> 两次。 - pg1989
@pg1989 没问题,标准头文件有包含保护或等效机制。 - M.M
4个回答

116

这是一个简单的问题。

你只是忘了在头文件末尾加上分号。

如果你在类定义的末尾没有加上分号,编译器返回的错误提示非常难以与实际问题相关联 - 请养成一个习惯,每次创建类后都检查一下是否有这个错误。


9
另一种产生此错误的方法:定义一个宏为字符串常量,之后将宏名用作字符串常量的名称。示例:
#define FOO "bar"
static const char FOO[] = "bar"; // <-- Error "expected unqualified-id before string constant".

明显的解决方法是删除其中一个定义或更改名称。

3

你的代码存在多个问题:

  • 在oscillators.cpp文件中,你需要使用完全限定名称(void oscillators::print_table() 而不是简单的 void print_table()
  • 你可能需要将#include "oscillators.h"添加到oscillators.cpp文件中
  • 你需要在实现中正确声明变量。
  • 添加缺少的分号。

但我猜测这个具体错误是由头文件中类定义后缺少分号造成的。只需像下面这样添加即可:

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

0
对我来说,这种情况曾经发生过一次,因为在早期的头文件中声明了同名的重复标识符。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接