字符串常量前应为非限定id 我目前正在编写一个C++应用程序,它实现了与Maj.h相结合的振荡器。我的代码应该可以很好地用于应用程序(尝试编译一个对象文件),但是我遇到了一个编译器错误,很可能与语法等有关;我认为这与名称空间有关。错误:

字符串常量前应为非限定id 我目前正在编写一个C++应用程序,它实现了与Maj.h相结合的振荡器。我的代码应该可以很好地用于应用程序(尝试编译一个对象文件),但是我遇到了一个编译器错误,很可能与语法等有关;我认为这与名称空间有关。错误:,c++,namespaces,g++,math.h,object-files,C++,Namespaces,G++,Math.h,Object Files,终端输出: 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/in

终端输出:

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
振荡器

#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”
#包括
#包括
#包括
#定义TWOPI(6.2831853072)
使用名称空间std;
振荡器(int srate=44100,int tabsize=8192,double freq=200)//默认输出200Hz正弦波
{

如果(srate这是一个简单的问题

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


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

您的代码存在多个问题:

  • 您需要在monitors.cpp中完全限定名称(
    void monitors::print_table()
    ),而不仅仅是
    void print_table()
  • 您可能需要
    #将“monitors.h”
    包含到monitors.cpp中
  • 您需要在实现中正确声明变量
  • 添加缺少的分号
但我猜具体的错误是由于头文件中的类定义后缺少分号造成的。只需添加如下内容:

    std::vector<double> gtable_; // Will contain a wavetable with a guard point.
};
#endif
std::vector gtable_;//将包含一个带保护点的波表。
};
#恩迪夫

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

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

static const char FOO[]=“bar”;//包含两次,一次在头文件中,一次在源文件中。@pg1989很好,标准头文件有include-guard或等效文件
#define FOO "bar"
static const char FOO[] = "bar"; // <-- Error "expected unqualified-id before string constant".