expected specifier-qualifier-list before std - c++

typedef struct _stResult {
std::string x;
int y;
struct _stResult *next;
} strResult;
In this structure i am getting the following error expected specifier-qualifier-list before std. What does this error mean?

Did you forget to #include <string> ?
The compiler obviously doesn't recognize std::string as a type.

std::string is not declared. If you #include <string> at the top, the code compiles.

Related

Error C2062: type unexpected when referencing std::map

#include <string>
#include <map>
namespace myNamespace
{
struct MyStruct
{
static std::map<int, std::string> idNameMap;
// some other static properties
};
class MyClass
{
private:
void myMethod() {
std::map<int, std::string>& myMap = MyStruct::idNameMap; // C2062: type 'int' unexpected
for (auto& it : myMap)
{
// do some stuff with map values
}
}
};
}
I'm trying to reference the static map property in MyStruct but it is producing this error. I'm not sure if more context is needed, but if so please let me know.
Let's try again...
You probably have an incorrectly terminated definition somewhere in your code, just before an int variable, that is not included in your question.
I.e. with this addition to your posted code, you will get exactly the same compiler error:
#include <string>
#include <map>
double x = 42.0, // ERROR! incorrectly terminated before an 'int' variable
int a = 0;
namespace myNamespace
{
...

Error: Expected initializer before "file", including ifstream.

Not really sure what the error is here. This is standard file opening that I've used all the time before. The right things are being included. And it's just a regular ifstream. What is wrong with this?
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(){
struct item{
string item;
string type;
int price;
}
ifstream board;
board.open("messageBoard.txt");
}
wow! no one can notice that??!!
int main(){
struct item{ //
string item; // error C2580: redefinition of class name 'item'
string type;
int price;
} // missing a semicolon here `;`
you are using the class name as another identifier so you get a compile-time error redefinition
so you can make them different:
struct Item //
{
string item; // now it's ok Item is not item
string type;
int price;
};

Visual studio cannot determine the type of my global variables

What I have is :
#include "thread.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
vector<Requester*> requesters; //global
struct Requester {
vector<thread> t;
vector<int> tracks;
};
Then in my function I have:
void serviceQ(){
vector<Requester*> test = requesters; //error
}
The error is:
no suitable user-defined conversion from "std::vector<<error-type> *, std::allocator<<error-type> *>>" to "std::vector<Requester *, std::allocator<Requester *>>" exists
I'm very confused as to why this is. Why does it call my global variable an error type in the function? If I were to do something like:
void serviceQ(){
vector<Requester*> test;
//do some stuff
vector<Requester*> result = test; //no error
}
Then there is no error.
You need to define
vector<Requester*> requesters; //global
after the definition of struct Requester, as otherwise the compiler doesn't know what Requester* means when it attempts to define the corresponding vector<Requester*>. Alternatively, you can just declare
struct Requester;
above the line vector<Requester*> requesters;.

shared_ptr template argument invalid

I am trying to have a static method return a shared_ptr.
It is not compiling and is giving template argument 1 is invalid.
I can not figure out why this is.
Also, stack overflow says my post is mostly code and that I should add more detail. I don't know why this is, as being concise never hurt anyone. My problem is clear cut and and can be detailed easily.
Compiler error
src/WavFile.cpp:7:24: error: template argument 1 is invalid
std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)
WavFile.cpp
#include "WavFile.h"
#include "LogStream.h"
#include "assert.h"
using namespace WavFile;
std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string filename)
{
ifstream infile;
infile.open( filname, ios::binary | ios::in );
}
WavFile.h
#pragma once
#ifndef __WAVFILE_H_
#define __WAVFILE_H_
#include <fstream>
#include <vector>
#include <memory>
namespace WavFile
{
class WavFile;
}
class WavFile::WavFile
{
public:
typedef std::vector<unsigned char> PCMData8_t;
typedef std::vector<unsigned short int> PCMData16_t;
struct WavFileHeader {
unsigned int num_channels;
unsigned int sample_rate;
unsigned int bits_per_sample;
};
static std::shared_ptr<WavFile> LoadWavFromFile(std::string filename);
private:
WavFile(void);
private:
WavFileHeader m_header;
PCMData16_t m_data16;
PCMData8_t m_data8;
};
#endif
Change the namespace name to something else. Now it clashes with the class' name, since you are using namespace WavFile;. Simple example that illustrates the error:
#include <iostream>
#include <memory>
namespace X // changing this to Y makes the code compilable
{
class X{};
}
using namespace X; // now both class X and namespace X are visible
std::shared_ptr<X> v() // the compiler is confused here, which X are you referring to?
{
return {};
}
int main() {}
If you insist to have the namespace named as the class, then get rid of the using namespace X; and qualify the type, std::shared_ptr<WafFile::WavFile>.

Deque of user-defined structures

I've got a user-defined structure struct theName and I want to make a deque of these structures (deque<theName> theVar). However when I try to compile I get this error:
In file included from main.cpp:2:
Logger.h:31: error: ISO C++ forbids declaration of ‘deque’ with no type
Logger.h:31: error: expected ‘;’ before ‘<’ token
Why can't I do it this way?
File: Logger.h
#ifndef INC_LOGGER_H
#define INC_LOGGER_H
#include <deque>
#include "Motor.h"
struct MotorPoint {
double speed;
double timeOffset;
};
class Logger{
private:
Motor &motor;
Position &position;
double startTime;
(31) deque<MotorPoint> motorPlotData;
double getTimeDiff();
public:
Logger(Motor &m, Position &p);
//etc...
};
#endif
The namespace of deque is not defined:
std::deque<MotorPoint> motorPlotData;
or
using namespace std;
// ...
deque<MotorPoint> motorPlotData;
deque is in namespace std, so std::deque.