C++ 字符串常量前的预期非限定 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11837024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:36:40  来源:igfitidea点击:

expected unqualified-id before string constant

c++namespacesg++math.hobject-files

提问by DanMoore

I'm currently writing a C++ application which implements an Oscillator in conjuction with math.h. The code I have should work fine for the application (trying to compile an object file), bu I'm getting a compiler error most likely having to do with syntax/etc; I think it has something to do with namespace. The error:

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

Terminal Output:

终端输出:

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

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

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

I've seen other suggestions that mention using g++ -Wall -g but I'm not sure that that is the problem, and that there is something else going on here.

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

Any help would be much appreciated! Thanks!!

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

回答by John Humphreys - w00te

This is a simple problem.

这是一个简单的问题。

You just forgot the semi colon at the end of your header file.

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

The compiler errors you get for missing the semi colon at the end of a class definition are very hard to relate to the actual problem - just get in the habit of checking that when you get errors after you create a class.

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

回答by bleater

Another way to produce this error: define a macro to a string constant, and later, use the macro name as the name of a string constant. Example

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

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

The obvious answer is to remove one of the definitions, or change the name of one.

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

回答by Alexander Putilin

Your code has multiple problems:

您的代码有多个问题:

  • You need to fully qualify names(void oscillators::print_table()instead of just void print_table()) in oscillators.cpp
  • You probably need to #include "oscillators.h"into oscillators.cpp
  • You need to properly declare variables in implementation
  • Add missing semicolons.
  • 您需要在 Oscillators.cpp 中完全限定名称(void oscillators::print_table()而不仅仅是void print_table()
  • 你可能需要#include "oscillators.h"进入oscillators.cpp
  • 您需要在实现中正确声明变量
  • 添加缺少的分号。

But I guess that specific erroris caused by missing semicolon after class definition in header file. Just add it like:

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

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