2012-08-06 121 views
14

我目前在寫一個C++應用程序,它實現了一個與math.h結合的振盪器。我的代碼應該可以正常工作(嘗試編譯一個目標文件),但是我得到的編譯器錯誤很可能與語法/ etc有關;我認爲這與名稱空間有關。錯誤:預期字符串常量之前的非限定符號

端子輸出:

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 - 克,但我不確定那是否是問題,並且這裏還有其他事情要做。

任何幫助將不勝感激!謝謝!!

+0

您包含兩次,一次在您的標題中,一次在源文件中。 – pg1989 2012-08-06 22:54:22

+0

@ pg1989很好,標準頭文件包含守衛或等效 – 2014-12-22 08:56:27

回答

64

這是一個簡單的問題。

您只是在頭文件末尾忘記了分號。

在類定義末尾丟失分號的編譯器錯誤很難與實際問題相關聯 - 只是習慣於在創建類後發現錯誤。

2

你的代碼有多種問題:

  • 您需要完全限定在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 
3

另一種方式來產生這個錯誤:定義一個宏字符串常量,後來,使用宏名作爲字符串常量的名稱。示例

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

顯而易見的答案是刪除其中一個定義,或更改其中一個名稱。

相關問題