Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Visual studio this declaration has no storage class or type specifier with vec. How do I fix this?
C++
#include 
#include 
class Element {
public: Element(int i, const std::string& str) : mI(i), mStr(str) { }
private: int mI;    std::string mStr; };  
std::vectorvec;  /*Next adding a new "Element" to vector*/  
Element myElement(12, "Twelve");  
vec.push_back(myElement);  <---


What I have tried:

Visual studio this declaration has no storage class or type specifier with vec. How do I fix this?
C++
#include 
#include 
class Element {
public: Element(int i, const std::string& str) : mI(i), mStr(str) { }
private: int mI;    std::string mStr; };  
std::vectorvec;  /*Next adding a new "Element" to vector*/  
Element myElement(12, "Twelve");  
vec.push_back(myElement);  <---
Posted
Updated 15-Nov-20 20:16pm
v2
Comments
jeron1 15-Nov-20 18:12pm    
Hide   Copy Code
std::vectorvec; /*Next adding a new "Element" to vector*/

This line looks incorrect.
Afzaal Ahmad Zeeshan 15-Nov-20 19:17pm    
I think there is a missing space there, it might be std::vector vec.
jeron1 15-Nov-20 20:05pm    
Yes, but you have to specify a type when declaring a vector variable, something like;
std::vector<Element> vec; 

See vector::vector - C++ Reference[^] for details.

1 solution

This is very, very basic stuff. You REALLY need to check out the tutorials I posted a link to previously. While you're at it, bookmark this site : cplusplus.com - The C++ Resources Network[^]
C++
std::vectorvec; /*Next adding a new "Element" to vector*/
Element myElement(12, "Twelve");
vec.push_back(myElement); <---
Yes, EXACTLY !!! what does that line with the arrow do? It references the push_back method of a vec object. Where is that variable declared? Look for it. We'll wait.

Look at the first line of that code. What does it do? Nothing, and that's the problem. It is not a function prototpe because there are no parenthesis. It is not a valid declaration because it lacks a type and name. That is the cause of the error because that is a mal-formed statement. You do know what a std::vector is, right? You are also missing the declaration of a variable called vec.

You will have to use your deductive reasoning to come up with the answer from here.
 
Share this answer
 
Comments
CPallini 16-Nov-20 2:16am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900