2012-08-06 16 views
14

私は現在、math.hと組み合わせてオシレータを実装しているC++アプリケーションを作成しています。私が持っているコードは、アプリケーション(オブジェクトファイルをコンパイルしようとしています)でうまく動作するはずです。buコンパイラエラーが発生する可能性があります。私は名前空間と関係があると思う。エラー:文字列定数の前にunqualified-idが必要です

ターミナル出力:私は使用して言及し、他の提案を見てきました

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 

グラム++ -Wall - しかし、私はそれが問題であるとは確信していないし、ここで何か他のことが起こっている。

ご協力いただければ幸いです。ありがとう!!

+0

をヘッダーに1回、ソースファイルに1回ずつ含めます。 – pg1989

+0

@ pg1989これは問題ありません。標準ヘッダーにはガードまたは相当品が含まれています –

答えて

64

これは簡単な問題です。

ヘッダーファイルの最後にセミコロンを忘れました。

クラス定義の最後にセミコロンがないために発生するコンパイラエラーは、実際の問題に関連づけるのが非常に難しいです。クラスを作成した後にエラーが発生したときにそれを確認するだけです。

2

あなたのコードは複数の問題があります。

  • あなたは完全に名前(void oscillators::print_table()だけではなくvoid print_table())を修飾する必要がありoscillators.cpp
  • にあなたはおそらくあなたが必要
  • oscillators.cppに#include "oscillators.h"する必要性を実行時に変数を正しく宣言する
  • 不足しているセミコロンを追加します。

は、しかし、私は特定のエラーは、ヘッダー・ファイル内のクラス定義の後にセミコロンが欠落によるものであると推測します。同じように、それを追加します。このエラーを生成する

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". 

明白な答えは、定義の1つを削除するか、または名前の名前を変更することです。

関連する問題