Ive looked all over and I cant find anything relating to my problem. Im trying to write a class definition for a polygon class that basicly has a vector that holds pointers to a point. When I try to compile i keep geting the folllowing errors...
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed.
error C2238: unexpected token(s) preceding ';'
error C2061: syntax error : identifier 'vector'
error C2065: 'myPolygonPoints' : undeclared identifier
error C2065: 'points' : undeclared identifier
error C2065: 'myHasInstersection' : undeclared identifier
error C2660: 'Polygon::addSetOfPoints' : function does not take 1 arguments
Here is the code to the class
#include "Point.h"
#include <vector>
class Point;
class Polygon
{
private:
vector<Point*> myPolygonPoints;
bool myHasIntersection;
public:
void addSetOfPoints(vector<Point*> points)
{
myPolygonPoints = points;
}
bool getHasIntersection()
{
return myHasIntersection;
}
void setHasIntersection(bool intersection)
{
myHasInstersection = intersection;
}
};
You are using vector from the std namespace without qualifying it.
You either have to do using namespace std;, or using std::vector, or declaring all your vector objects with the std namespace like std::vector.
#include "Point.h"
#include <vector>
class Point; // Assuming Point.h has this declared,
// you don't need this forward declaration, but in reality,
// you don't need to include Point.h
// since you're only declaring pointers to Points
class Polygon
{
private:
std::vector<Point*> myPolygonPoints;
bool myHasIntersection;
public:
void addSetOfPoints(std::vector<Point*> points)
{
myPolygonPoints = points;
}
bool getHasIntersection()
{
return myHasIntersection;
}
void setHasIntersection(bool intersection)
{
myHasInstersection = intersection;
}
};
vector is in the std:: namespace. so vector is undefined in your example code
Two possible solutions:
#include <vector>
using std::vector;
or: (in all cases through the code where you reference vector, both declaration and reference)
private:
std::vector<Point*> myPolygonPoints;
public:
void addSetOfPoints(std::vector<Point*> points)
etc.
A third solution is the following:
#include <vector>
using namespace std;
This last one, from a coding style perspective, I find less preferred. The reason is that it imports absolutely everything in the std namespace into the default namespace. By contrast, I find it preferrable to explicitly import the pieces I'm using, becuase it allows me to keep track of why I need a header. This doesn't make a sense in this case (of course I need <Vector>, I'm using std::vectors). It's much more relevant in a case like this:
#include <algorithm>
using std::adjacent_find;
Oh yeah, that's why I included that...
If you're not explicitly declaring that you're using the std namespace you should reference which namespace vector belongs to.
Besides the std::vector problem, you have also mispelled the myHasIntersection variable in the setHasIntersection method.
Related
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 new to C++ and I am trying to make a little dungeon crawler game. Currently I have multiple vectors declared in my header files but they seem to give multiple errors. I have tried searching for this problem on StackOverflow but the answers don't really seem to work.
Here is one of my header files: (Hero.h)
#pragma once
class Hero {
public:
Hero();
std::string name;
int experience;
int neededExperience;
int health;
int strength;
int level;
int speed;
std::vector<Item> items = std::vector<Item>();
void levelUp();
private:
};
Here is my .cpp file: (Hero.cpp)
#include "stdafx.h"
#include <vector>
#include "Hero.h"
#include "Item.h"
Hero::Hero() {
}
void Hero::levelUp()
{
};
Like I said I am new to C++ so there might be a lot more wrong with my code than I know. This is just a test.
Below are the errors that are shown in the Error list of Visual Studio 2015:
Error C2039 'vector': is not a member of 'std' CPPAssessment hero.h 13
Error C2143 syntax error: missing ';' before '<' CPPAssessment hero.h 13
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int CPPAssessment hero.h 13
Error C2238 unexpected token(s) preceding ';' hero.h 13
Include <vector> in your Hero.h header and consider removing it from your Hero.cpp file as mentioned in the comments below.
std::vector<Item> items = std::vector<Item>(); declares a complete type.
Therefore the compiler needs to know the declaration of std::vector at that point (amongst other things, it's required to establish the compile-time evaluable constant sizeof Hero). The solution is to #include <vector> in the header hero.h, not the source file.
//.h
template<class _Ty>
class std::allocator;
template<class _Ty, class _Alloc = std::allocator<_Ty> >
class std::vector;
//this line in .cpp file. Put it before custom header files.
#include<vector>
Tested in vs 2017.
In this way, no header file included in your custom header file.
But not sure why the save way for stack does not work.
For further information, https://github.com/YagaoDirac/snippet-set-for-cpp. And a discord link inside.
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> ?
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.