Defining a struct in flex error C++ - c++

I want to define a struct in a flex program :
%{
#include <math.h>
#include <string>
#include <vector>
#include <iostream>
#include <map>
struct Node{
string action;
vector<Node> vecini[];
};
%}
and even though I include vector and string I still get this errors:
error: ‘string’ does not name a type
error: field ‘vecini’ has incomplete type
Thank you !

Both vector and string are in the std namespace so you should add it to the declaration of member variables of those types. Change the code to:
%{
#include <math.h>
#include <string>
#include <vector>
#include <iostream>
#include <map>
struct Node{
std::string action;
std::vector<Node*> vecini[];
};
%}
EDIT: (thanks to Kerrek SB): also you can not define a vector of Node as a member of Node. Instead use a vector of pointers to node like so: std::vector<Node*> vecini[];

Use fully qualified names for string and vector, for example:
std::string action;
^^^^^^^^^^^
Note that, string and vector are defined only in the std namespace, so you need to specify the fully qualified name to tell the compiler under which namespace to find them.

Related

N does not name a type

// q.h file
#ifndef __Q_H__
#define __Q_H__
using namespace std;
#include "n.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
class Q
{
public:
Q();
private:
N* beginning; //error N does not name a type
N* end; //error N does not name a type
int count;
};
#endif // end of file
// q.cpp file
#include "q.h"
#include "n.h"
#include <iostream>
using namespace std;
#include<string>
Q::Q()
{
beginning = NULL;
end = NULL;
}
// n.h file
#ifndef _N_H__
#define _N_H__
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include "q.h"
class N
{
public:
N(int);
// next is a pointer object of type N
N* next;
// memeber for Node class
int value;
};
#endif
// n.cpp file
#include<string>
#include "q.h"
#include "n.h"
N::N(int v)
{
value = v;
}
I get an error at the following lines below. I tried changing user namespace std; to the first line of each file but that still does not work. I've also tried changing the order of q.h and n.h in which they are presented but still nothing.
N* beginning;
N* end;
Also I know one great solution is by using "forward declare" but this is part of a test file that i given to me by someone so I cannot do a forward declaration of a class.
So if anyone can please just take a look and see how I can fix this.
Thanks.
You have a circular reference (q.h includes n.h and n.h includes q.h). This error occurs because the compiler is including (maybe because the main program) q.h before than n.h. Thus, q.h need to include an empty class declaration as follows to Q knows about N.
...
#include <string>
class N;
class Q
{
...
If you can't edit those files, you still can add the empty class declaration anywhere before the "Q" class declaration (e.g. before including q.h in your main program). However, the class N don't need anything from Q, so you don't need to include q.h in n.h.
Since #include's are done in a way, where the preprocessor just takes the file that you are #include'ing and copy-pastes in the spot of #include statement, when n.h is being processed, it includes q.h, and then class Q appears before class N. Hence, it doesn't know what the class N is, since the compiler works in linear fashion (from the start of the file to the end).
So, it is my suggestion, to remove #include "q.h" statement from n.h, since the class N doesn't use class Q.

Invalid Template Argument for vector instantiation

When I run this code I get "invalid template arguments" error on the last line. Please advise. (I've omitted the rest of the code)
#include <iostream>
#include <fstream>
#include<array>
#include <vector>;
using namespace std;
int fileLineCount(string);
int fileExists(string[],int);
int main() {
ifstream archiveFile;
archiveFile.open("StudentRecords.txt");
int lineCount=fileLineCount("StudentRecords.txt");
string line;
vector<string> recordArray;
#include <vector>;
should be
#include <vector>
and of course you need to close the } brace at the end of main() (although probably this was a typo). You should also #include <string>, although some of your headers seem to include it implicitly (probably <iostream>).

Class differences between C++03 and C++11

I'm current building an application in which I have a log function that is accessible in most of my classes which was declared as below:
FileHandler.h
#ifndef FILEHANDLER_H
#define FILEHANDLER_H
#pragma once
#include <SDL.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <cctype>
//Include to allow logging
#include "log.h"
class fileHandler
{
public:
fileHandler();
virtual ~fileHandler();
void WriteToFile(const std::string& filename, std::string textToWrite);
std::vector<std::string> ReadFromFile(const std::string& filename);
std::string& TrimString(std::string& stringToTrim);
protected:
private:
class log logHandler;
std::vector<std::string> blockOfText;
std::string currentLine;
};
#endif // FILEHANDLER_H
Log.h
#ifndef LOG_H
#define LOG_H
#pragma once
#include <SDL.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
class log
{
public:
log();
virtual ~log();
void static WriteToConsole(std::string textToWrite);
void WriteToLogFile(std::string textToWrite);
protected:
private:
};
#endif // LOG_H
This worked fine for a long time and then I wanted to include another function elsewhere in my application that was only compatible with C++11 so I told the compiler to compile to these standards. I was then receiving an error on "log logHandler" saying log is not a declared name.
I was able to resolve the problem by changing the line to
class log logHandler;
I was wondering if anybody could tell me what has changed between C++03 and C++11 that required me to do this?
EDIT: Included all relevant code to make question more complete.
You don't show your real code (missing ; at the end of the class declaration, no #endif), but chances are that your problem is somehow related to std::log, which has received a new overload in C++11, in combination with a using namespace std somewhere in your code.
Note that the new overload is probably irrelevant to the problem at hand; the real reason may very well be a change somewhere in your compiler's standard-library implementation causing an internal #include <cmath>. This means that even in C++03, your code was only working by sheer coincidence, and a conforming C++03 compiler was always allowed to reject it.
Here is an example program which may reproduce your problem:
#include <cmath>
using namespace std;
struct log
{
};
int main()
{
// log l; // does not compile
struct log l; // compiles
}
Nothing has changed about how the code you posted is treated.
What I suspect is, that you somewhere have an
#include <cmath>
And below that, somewhere else
using namespace std;
This causes your compiler to not be able to unambiguously resolve the name log, since there is std::log (a function) and your class log.
By explicitly stating class log, you tell the compiler that you are referring to the class.

Why am I getting string does not name a type Error?

game.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
game.h
#ifndef GAME_H
#define GAME_H
#include <string>
class Game
{
private:
string white;
string black;
string title;
public:
Game(istream&, ostream&);
void display(colour, short);
};
#endif
The error is:
game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type
Your using declaration is in game.cpp, not game.h where you actually declare string variables. You intended to put using namespace std; into the header, above the lines that use string, which would let those lines find the string type defined in the std namespace.
As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead
string does not name a type. The class in the string header is called std::string.
Please do not put using namespace std in a header file, it pollutes the global namespace for all users of that header. See also "Why is 'using namespace std;' considered a bad practice in C++?"
Your class should look like this:
#include <string>
class Game
{
private:
std::string white;
std::string black;
std::string title;
public:
Game(std::istream&, std::ostream&);
void display(colour, short);
};
Just use the std:: qualifier in front of string in your header files.
In fact, you should use it for istream and ostream also - and then you will need #include <iostream> at the top of your header file to make it more self contained.
Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.
The namespace in game.cpp is after the header is included.
You can overcome this error in two simple ways
First way
using namespace std;
include <string>
// then you can use string class the normal way
Second way
// after including the class string in your cpp file as follows
include <string>
/*Now when you are using a string class you have to put **std::** before you write
string as follows*/
std::string name; // a string declaration

How to declare vectors in C++?

I'm trying to use a vector of strings in my code instead of an array of strings but apparently I miss some detail in the declaration of the vector. Using the following code, I get this error: ‘vector’ was not declared in this scope
// Try to implement a vector of string elements
#include<iostream>
using namespace std;
int main() {
const int MAX_ITEMS = 10;
vector<string> my_vector(MAX_ITEMS);
return 0;
}
How should I correctly declare the vector?
You should add these includes:
#include <vector>
#include <string>
You have to include the header:
#include <vector>
#include <string>
You need:
#include <vector>