Compiler error with list<string> - c++

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");
}

Related

missing type specifier - int assumed. Note c++ does not support default-int

Getting this error along with an error stating that the variables "managers", "stores" "products", and inventory" are undeclared and there are missing ';' before them.
#include "Manager Info Structure.h"
#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
#include <array>
using namespace std;
//Vendor Variables
int VENDORSIZE = 3;
ManagerData managers[3];
the structure is listed here:
#pragma once
struct ManagerData
{
int man_iD;
string man_fname;
string man_lname;
int store_num;
int exp_mon;
int exp_year;
string man_phone;
string man_email;
};
edit: removed superfluous information, attempting the put pch.h at the top of the solution results in 3 instances of the error listed in the title of this post along with 'man_fname': unknown override specifier, 'man_lname': unknown override specifier, 'man_phone': unknown override specifier, 'man_email': unknown override specifier. Thank you everyone for your answers so far
You are most probably missing a ; after definition of one of the mentioned structs.

C++ - Visual Studio - missing type specifier - int assumed

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.

Unknown override specifier, missing type specifier

First, Parameter.h:
#pragma once
#include <string>
class Parameter {
public:
Parameter();
~Parameter();
private:
string constValue;
string varName;
};
And Parameter.cpp:
#include "Parameter.h"
using namespace std;
Parameter::Parameter() {};
Parameter::~Parameter() {};
I've brought these two files down to the barest of bones to get the errors that seem to be popping up. At the two private declarations for strings, I get the two errors:
'constValue': unknown override specifier
missing type specifier - int assumed. Note: C++ does not support default-int
I've seen several questions with these errors, but each refers to circular or missing references. As I've stripped it down to what's absolutely required, I can see no circular references or references that are missing.
Any ideas?
As #Pete Becker points out in the comments, you need to qualify the name string as std::string:
private:
std::string constValue;
std::string varName;
The compiler just doesn't know what you're talking about, and it's the equivalent of just writing:
SomeGreatType myMagicalUniversalType
The compiler just doesn't know what type that is unless you've declared, hence the error
missing type specifier - int assumed
You should read up about why you should avoid using namespace std;.
With regards to your question in the comments:
In all the (working) classes I've written, I've never put in the std::, instead relying on the using namespace std; in the .cpp file. Why would this one be any different?
I can only infer that at some point before including "Parameter.h" that you had a using namespace std. E.g.:
// SomeType.h
#using namespace std
...
// Parameter.cpp
#include "SomeType.h"
#include "Parameter.h"
The compiler compiles things top-to-bottom, and including essentially just replaces the #include with the contents of that file

C++ can't create vector

This is weird. I created a vector just fine in one class but can't create it in another class. He's a representation of what I have:
main.h
#include <Windows.h>
#include <ShellAPI.h>
#include <vector>
#include <string>
#include <iostream>
#include "taco.h"
class MyClass
{
public:
int someint;
vector<int> myOrder;
};
taco.h
#include <vector>
class OtherClass
{
public:
vector<int> otherOrder;
};
And I get compile errors regarding the vector declaration in taco.h:
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
What am I missing here? I can uncomment out that second vector declaration and it compiles fine.
Try:
std::vector<int> otherOrder;
vector is part of the std namespace. This means that whenever you use a vector in a header file, you should include the std:: prefix.
The reason that you can sometimes get away with forgetting it is that some included files may have using namespace std; in them, allowing you to leave off the prefix. However, you should avoid the using keyword in header files, for it will pollute the namespace of any files that include it.
For a more detailed explanation of the dangers of using namespace ..., see this thread.
Try std::vector<int>. You're supposed to use the namespace --- I'm assuming you have
using namespace std;
in the main.h someplace. There's a lot of talk on SO as to why using using is bad practice ; I'd recommend that you check it out.
All C++ standard library objects live in the std namespace. Try
class MyClass
{
public:
int someint;
std::vector<int> myOrder;
// ^^^^^
};
std::vector<int> ?

compiling error with vector in c++

I'm stuck! I have this very simple test code and I can't get it to compile! I have used the same code many times before but now it won't work!
I have this simple program
#include <vector>
#include <iostream>
#include "Rswap.h"
using namespace std;
int main(){
Rswap test();
cin.get();
return 0;}
And then the rswap.cpp...
#include <vector>
#include "Rswap.h"
Rswap::Rswap(){
V.push_back(9);
};
And then the rswap.h...
#ifndef Rswap_h
#define Rswap_h
class Rswap{
public:
vector<int>V;
Rswap();
};
#endif
I'm using Visual studio 2008. Is there something wrong that is obvious and I'm missing or what could it be! As I said I have used this snippet on several differnet occassions and I can't find any difference between this and those that work...
And i have tried both
vector < int > V; and vector <int> V; without any luck
I have stared at this blindly now for some while so I thought it's better to ask here!
rswap.h(7) : error C2143: syntax error : missing ';' before '<'
rswap.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
rswap.h(7) : error C2238: unexpected token(s) preceding ';'
At the point you #include "Rswap.h", you haven't declared using namespace std; yet, so the reference to vector in Rswap.h must be qualified with a namespace. Simply declare the vector with a namespace prefix.
class Rswap{
public:
std::vector<int>V;
Rswap();
};
Also, I suggest you #include <vector> from Rswap.h rather than relying on whoever uses Rswap.h to get the #include order right.
It should be
std::vector<int>V;
Ah, your using namespace std comes too late - it should be inserted BEFORE the rswap.h include. In any case, it's MUCH better to write std::vector instead.
You need to #include <vector> in rswap.h.