I am trying to combine using a std::set and a class, like this:
#include <set>
class cmdline
{
public:
cmdline();
~cmdline();
private:
set<int> flags; // this is line 14
};
but, it doesn't like the set flags; part:
cmdline.hpp:14: error: ISO C++ forbids declaration of 'set' with no type
cmdline.hpp:14: error: expected ';' before '<' token
make: *** [cmdline.o] Error 1
As far as I can see, I gave a type (of int). Do you write the "set variable" line differently, or is it not allowed there?
You need to use std::set; set is in the std namespace.
You mean std::set.
You have to use the std:: namespace (for all STL/SL classes).
std::set< int > myset;
or
using namespace std; // don't do this in a header - prefer using this in a function code only if necessary
Related
I'm learning c++ and was playing around with macros. I tried defining push_back as pub, and it gave me this error:
error: reference to non-static member function must be called
vect.pub(1);
Here's my code:
#include <vector>
using namespace std;
typedef vector<int> vi;
#define pub push_back;
int main(){
vi vect;
vect.pub(1);
}
When I didn't use the #define and just wrote push_back, there was no error messages.
What exactly changed when I used the macro?
#define pub push_back;
//...
vect.pub(1);
This expands to the following, which is invalid syntax due to the extra ;.
vect.push_back;(1);
So drop the ; and #define pub push_back.
You should not put ';' for macro.
#include <vector>
using namespace std;
typedef vector<int> vi;
#define pub push_back
int main(){
vi vect;
vect.pub(1);
}
I'm learning c++ and was playing around with macros.
Stop. push_back is at most 6 extra keystrokes. Code is meant to be read by humans. You can't find pub in documentation, but you can find push_back.
Similarly using namespace std; is a terrible habit. There are loads of names that you don't realise you've just imported into the global namespace there.
A very basic question, but i haven't found the answer yet.
I wrote an Application which is using an Array with a constant predefined Value which defines the size of this array. So but now i want to change this so that the "list" can theoretically be endless (i know that it would be practically impossible). For that I want to use a vector. But when I type the following it gives me an Error:
edit(2): Wrote accidentially const and a wrong parameter for push_back funktion, heres the final version, which gives the error.
#include "stdafx.h"
#include "string"
#include "vector"
using namespace std;
struct Board {
vector <string> myVector;
myVector.push_back("foo");
};
Error Message:
<error-type> Board::myVector
This declaration has no storage class or type specifier.
My thought was that vectors dont work in structs. I heard that structs are a plain C thing and vectors are more a C++ thing, maybe thats why it is so? But actually I have no clue, thats why I am asking here :)
edit (1):
I just gave you the visual studio error, maybe I should give you the compiler errors..:
error C3927: '->': trailing return type is not allowed after a non-function declarator
error C3484: syntax error: expected '->' before the return type
error C3613: missing return type after '->' ('int' assumed)
error C3646: 'push_back': unknown override specifier
error C2059: syntax error: '('
error C2238: unexpected token(s) preceding ';'
The problem are as followings:
(corrected in OP question) myVector is defined const
myVector.push_back(1); is not in any function body.
(corrected in OP question) Value passed to myVector.push_back(1); is int but vector is of type string
Change it as following. See example program working here:
#include "string"
#include "vector"
#include "iostream"
using namespace std;
struct Board {
vector<string> myVector;
void push_back(string val)
{
myVector.push_back(val);
}
void print()
{
for (auto it = myVector.begin(); it != myVector.end(); ++it)
cout << " | " << *it;
}
};
int main()
{
Board b;
b.push_back("Value 1");
b.push_back("Value 2");
b.print();
return 0;
}
UPDATE:
(can you actually use push_back for a vector in a struct without creating an extra function?)
No. structure can have only data members and member functions. but you can use initializer-list to initialize vector as following:
vector<string> myVector{"IVal 1", "IVal 1"};
If you wants to put the initlize value always at the end then use vector.insert() instead of vector.push_back().
class Space2D {
public:
vector<Agent> v;
bool star;
Space2D() {
bool star = false;
}
};
In visual studio this give me a error: missing type specifier - int assumed. I also get errors like syntax error: missing ';' before '<'.
std::vector<Agent> v; Solved this
Three possible problems with this code.
Possibly vector header is not included
Fix: #include <vector>
Possibly Agent class is not defined
Fix: include header, where Agent class is defined
Possibly you forgot to write using namespace std; as wrongly recommended by beginner books
Fix: instead of 'vector' on line 7 use std::vector, or do it wrong and write using namespace std;
Assuming from two error messages you posted and by guessing to which line they correspond to you forget to #include <vector> or you do not have imported std::vector to your namespace (using std::vector; or using namespace std).
I personally would not recommend using either of those usings for reasons and instead wrote std::vector.
I am doing everything correctly as far as I can tell and I have gotten the error message:
error: 'unordered_map' does not name a type
error: 'mymap' does not name a type
In my code, I have:
#include <unordered_map>
using namespace std;
//global variable
unordered_map<string,int> mymap;
mymap.reserve(7000);
void main {
return;
}
I don't see what can be missing here....
EDIT: when I update my declaration to
std::tr1::unordered_map<string,int> mymap;
I an able to eliminate the first error, but when I try to reserve, I still get the second error message.
EDIT2: As pointed out below, reserve must go into main and I need to compile with flag
-std=c++0x
However, there still appear to be errors related to unordered_map, namely:
error: 'class std::tr1::unordered_map<std::basic_string<char>, int>' has no member named 'reserve'
Compile with g++ -std=c++11 (my gcc version is gcc 4.7.2) AND
#include <unordered_map>
#include <string>
using namespace std;
//global variable
unordered_map<string,int> mymap;
int main() {
mymap.reserve(7000); // <-- try putting it here
return 0;
}
If you want to support <unordered_map> for versions older than c++11 use
#include<tr1/unordered_map> and declare your maps in the form :- std::tr1::unordered_map<type1, type2> mymap
which will use the technical report 1 extension for backward compatibility.
You can't execute arbitrary expressions at global scope, so you should put
mymap.reserve(7000);
inside main.
This is also true for other STL containers like map and vector.
I'm trying to create a list of strings, following the example here. This below gives me syntax errors:
private: list<string> images;
The errors (all on the line where the above declaration is):
syntax error : missing ';' before '<'
missing type specifier - int assumed. Note: C++ does not support default-int
unexpected token(s) preceding ';'
It's in a class with only a single constructor besides it, and it compiles fine without it. What am I doing wrong?
Did you #include both <list> and <string>? Also, did you import the names list and string from namespace std by writing either
using namespace std;
or
using std::list; using std::string;
The error you're getting is consistent with the names not being accessible, so this is my best guess.
EDIT: Since this is in a header file, you should not be using either of the above constructs (thanks to wilhelmtell for pointing out that this is a header file!). Instead, you should fully-qualify the names as
private: std::list<std::string> images;
This way the compiler knows exactly where to look for list and string.
You need to qualify the list and string types with their namespace.
Either type std::list<std::string> or add using namespace std; after the #include <string> and #include <list> directives.
A simple working program:
#include <list>
#include <string>
using namespace std;
int main ( int, char ** )
{
list<string> strings;
strings.push_back("1st string");
}