verwachte niet-gekwalificeerde id vóór tekenreeksconstante

Ik ben momenteel bezig met het schrijven van een C++-toepassing die een Oscillator implementeert in combinatie met math.h. De code die ik heb zou goed moeten werken voor de toepassing (proberen een objectbestand te compileren), maar ik krijg een compilerfout die waarschijnlijk te maken heeft met syntaxis/etc; Ik denk dat het iets met namespace te maken heeft. De fout:

Terminaluitgang:

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

Ik heb andere suggesties gezien waarin het gebruik van g++ -Wall -g wordt genoemd, maar ik weet niet zeker of dat het probleem is en dat hier iets anders aan de hand is.

Alle hulp wordt zeer op prijs gesteld! Bedankt!!


Antwoord 1, autoriteit 100%

Dit is een eenvoudig probleem.

Je bent de puntkomma aan het einde van je headerbestand vergeten.

De compilerfouten die je krijgt als je de puntkomma aan het einde van een klassedefinitie mist, zijn erg moeilijk te relateren aan het werkelijke probleem – maak er een gewoonte van om dat te controleren wanneer je fouten krijgt nadat je een klasse hebt gemaakt.

p>


Antwoord 2, autoriteit 6%

Een andere manier om deze fout te produceren: definieer een macro voor een tekenreeksconstante en gebruik later de macronaam als de naam van een tekenreeksconstante. Voorbeeld

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

Het voor de hand liggende antwoord is om een van de definities te verwijderen of de naam ervan te wijzigen.


Antwoord 3, autoriteit 3%

Je code heeft meerdere problemen:

  • U moet namen volledig kwalificeren(void oscillators::print_table()in plaats van alleen void print_table()) in oscillators.cpp
  • U moet waarschijnlijk #include "oscillators.h"in oscillators.cpp
  • U moet variabelen correct declareren in implementatie
  • Ontbrekende puntkomma’s toevoegen.

Maar ik vermoed dat specifieke foutwordt veroorzaakt door een ontbrekende puntkomma na klassedefinitie in het headerbestand. Voeg het gewoon toe als:

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

Other episodes