expected unqualified-id before string constant
Asked Answered
L

5

38

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:

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

#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

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.

Any help would be much appreciated! Thanks!!

Laynelayney answered 6/8, 2012 at 22:45 Comment(2)
You include <vector> twice, once in your header and once in your source file.Archenteron
@Archenteron that's fine, standard headers have include guards or equivalentExonerate
O
121

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.

Outtalk answered 6/8, 2012 at 22:53 Comment(2)
you saved hours of search man...Proselytism
I'm impressed that the systems don't flag this better after 12 years, haha. I'm pretty out of touch with C++ these days, so I don't know how things have changed.Outtalk
A
9

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.

Armure answered 22/12, 2014 at 8:4 Comment(0)
B
3

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.

But I guess that specific error is 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
Butterfat answered 6/8, 2012 at 22:57 Comment(0)
O
0

To me it happened once because of duplicate identified (with same name) declared in an earlier header.

Oxidate answered 26/11, 2021 at 18:3 Comment(0)
O
-1

placing a file that's supposed to have global scope inside any function, like main() can cause errors like this.

ie: placing

#include <math.h> 

inside the main function will cause this error. It also creates cascading errors further down that can show up as missing semi-colons in other unrelated areas of code.

Osteoblast answered 9/12, 2023 at 19:57 Comment(2)
incorrect answerIntransigeance
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Ahwaz

© 2022 - 2024 — McMap. All rights reserved.