What does having "< >" around a pointer mean/do? C++ - c++

I have my little code that needs to be solved/fixed but I'm struggling and getting stuck on problems too simple to be found with a simple Google search.
int vector, Shape, Shapes;
int Parameters;
int main()
{
string UserCommand;
vector <Shape*> Shapes;
vector <string> Parameters;
}
My question is what basically the <> around the pointer are for? What do they do? They give me the following error on <Shape*>:
expected an expression
Is something meant to follow this pointer?
Also, where vector <string> Parameters; is, I get the error:
type name not allowed
It's something to do with the <> symbols again.
What does one do when something is surrounded by <>?

FOUND THE ISSUE. I simply created another header and cpp file, then deleted them both and visual studio woke up and removed the error. Weird

Related

How can I initialize an array in C++ using a variable initialized at runtime? [duplicate]

This question already has an answer here:
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 12 months ago.
What i would like to do is declare an array with "dim" size :int A[dim];.
Now, this works if I declare something like const int dim = 1 but doesn't with const int dim = round(x);, which is what i need to do. (Where x comes from cin >> x.)
Note: With "doesn't work" i refer to Visual Studio Code throwing red wavy line under dim in int A[dim]; and displaying the following when hovering it with my mouse:
`
expression must have a constant valueC/C++(28)
main.cpp(15, 11): the value of variable "dim" (declared at line 13) cannot be used as a constant
`
This is the relevant code:
#include <iostream>
using namespace std;
int main(){
float x;
cin >> x;
const int dim = round(x);
int A[dim];
int i = 0;
}
}
Given the context i believe the error is caused by one of two reasons:
Some characteristic of round() that makes the const int dim = round(x) not recognized as constant from the array later.
The problem is the x and not the round() so cin >> x is the reason.
[Thanks for whoever can explain me what I'm missing or point to some documentation that does. I have done some research but I haven't found a solution to this. Also this is my first question on SO, so tell me if I should change/improve something]
EDIT: Apparently the problem isn't in the round(x) as I previously thought because simply replacing const int dim = round(x); with const int dim = x; gives the same "error".
So the problem has to do with cin >> x .
EDIT 2 Note: I'm looking for a solution that doesn't use std::vector. We haven't studied it yet in the course so I believe the algorithm(from which i took the relevant code) shouldn't comprehend it.
Final Edit I didn't realize that, as #paulmckenzie clarified, using cin made the array dynamic because the imput comes in runtime, it was a really stupid error but I apologize, I'm really a beginner. In my defense we really haven't talked about dynamic size arrays so I guess that's what threw me off. I realized from the beginning I was missing something very basic, sorry for wasting time, I'll put even more time analyzing everything before posting next time.
The size of an array variable must be compile time constant in C++. User input is not compile time constant, hence it cannot be used as the size of an array variable.
In order to create an array with runtime size, you must instead create a dynamic array. The simplest way to do that is to use std::vector from the standard library.
EDIT 2 Note: I'm looking for a solution that doesn't use std::vector.
It's possible to create a dynamic array without std::vector, but that requires the use and understanding of more advanced concepts. Using new expressions directly is more difficult, error prone and is something that isn't (or shouldn't) be done in most programs in practice.
Of course, another solution is to just not use user input but rather an array with constant size.

C++ unknown name type

I have a header file defining some parameters. I have defined some of the parameters as extern. My program works fine with other data types such as double and int, except when I try to add vector variables. The declaration in header file is
extern std::vector<double> my_vec;
In my main file, I am constructing the vector using this code:
std::vector<double> my_vec(3,0);
When I try to clear the vector using the clear method, the compiler is giving an error saying that unknown type. I am not even sure how to debug this. Can someone help?
P.S. I was originally trying to assign some values to this vector using:
my_vec[0] = 1;
but the compiler says that C++ requires a type specifier for all declarations. I googled this error, but I don't understand because I am specifying the type of my_vec.
Edit: example:
main.cpp
#include "params.h"
#include <vector>
std::vector<double> my_vec(3,0);
my_vec.clear();
// edit: my_vec[0] = 1; this also produces an error
int main(){
return 0;
}
params.h
#include <vector>
extern std::vector<double> my_vec;
Error message:
main.cpp:6:1: error: unknown type name 'my_vec'
my_vec.clear();
^
main.cpp:6:7: error: cannot use dot operator on a type
my_vec.clear();
^
2 errors generated.
You can't execute statements outside of a function - which is what you're trying to do with my_vec.clear();. It doesn't matter that clear() is a method of the vector class - invoking a method (as opposed to constructing a variable) is a statement, just like x = 1; . Those belong in functions.
You have to put your statement somewhere in your main(), e.g.:
int main(){
my_vec.clear();
return 0;
}
or make sure and construct my_vec the way you want it to look like, to begin with.
Also, more generally, you should avoid global variables if you don't really need them. And - you very rarely do. See:
Are global variables bad?
Edit: OP asks whether we can get around this restriction somehow. First - you really shouldn't (see what I just said). But it is possible: We can use a static block, which is implementable in C++, sort of.

swapping first and last element in stack c++ using stl

i created an account so i can get some help with stacks in STL , i need to write a function that takes stack as a parameter and swaps the first element with the last element , i searched the site for some help i found one :"https://stackoverflow.com/a/36188943/9990214" , i tried the same thing , but i keep getting this error : expression must have a constant value with red line under "int tmp[sz-1];".
it keeps giving me the error before reaching the main , any help would be appreciated , keep in mind am trying to write the function using STL.
ps : i tried replying with a comment to the person who answered the question but it's not allowing me to do that because i need 50 reputation.
using namespace std;
void rev(stack<int>&x){
int sz=x.size(),mytop,mybottom;
mytop=x.top();
x.pop();
int tmp[sz-1],i=0;
while(!x.empty()){
mybottom=x.top();
tmp[i++]=mybottom;
x.pop();
}
stack<int> returnIt;
returnIt.push(mybottom);
for(i=0;i<=sz-3;i++){
returnIt.push(tmp[i]);
}
returnIt.push(mytop);
while(!returnIt.empty()){
int tt=returnIt.top();
x.push(tt);
returnIt.pop();
}
}
The reason you're getting an error is that variable-length arrays are not a part of standard C++. This matters for your definition of tmp:
int tmp[sz-1], i=0; //sz is not known at compile-time, therefore, this is invalid code
Some compilers will allow code like this by allowing VLA's, but not being standard, you should use a different solution. Usually, for tasks like this, std::vector is ideal:
std::vector<int> tmp(sz - 1);
int i = 0;
This should compile (so long as you #include<vector> alongside your other includes), and should have the behavior you expect from your code.

Declaring a string array in class header file - compiler thinks string is variable name?

Hey everybody, I need a bit of a hand with declaring a string array in my class header file in C++.
atm it looks like this:
//Maze.h
#include <string>
class Maze
{
GLfloat mazeSize, mazeX, mazeY, mazeZ;
string* mazeLayout;
public:
Maze ( );
void render();
};
and the constructor looks like this:
//Maze.cpp
#include <GL/gl.h>
#include "Maze.h"
#include <iostream>
#include <fstream>
Maze::Maze( )
{
cin >> mazeSize;
mazeLayout = new string[mazeSize];
mazeX = 2/mazeSize;
mazeY = 0.25;
mazeZ = 2/mazeSize;
}
I'm getting a compiler error that says:
In file included from model-view.cpp:11:
Maze.h:14: error: ISO C++ forbids declaration of ‘string’ with no type
Maze.h:14: error: expected ‘;’ before ‘*’ token
and the only sense that makes to me is that for some reason it thinks I want string as a variable name not as a type declaration.
If anybody could help me out that would be fantastic, been looking this up for a while and its giving me the shits lol.
Cheers guys
You need to qualify the name: std::string.
Apart from that, you class design doesn’t separate concerns properly: the class should not ask user input directly. That should be a parameter to the class constructor – the actual input handling should be outside the class.
Furthermore, I hope you’re properly deleting the memory you allocated in the class destructor. It would be better not to use a raw pointer. Use a std::vector<std::string> instead. This is much easier and safer.
Use C++ STL SGI.
Vector documentation: http://www.sgi.com/tech/stl/Vector.html
String documentation: http://www.sgi.com/tech/stl/basic_string.html
You access vector like as normal array because he have overloaded [] operator.
But if you may add intems to a vector you must do it by the insert function, like that:
v.insert(v.end(), new_item);.
This code add a new_item at ends of the vector v.
Vector declaration you doing with std::vector<std::string>.

C++ problem with std::pair and forward declarations

Unfortunately I still got a problem with my templated code from here:
C++ fancy template code problem
on line 49 in the file 'utility':
error C2440: 'Initializing': cannot convert from 'const int' to 'IntersectionData *'
error C2439: 'std::pair<_Ty1,_Ty2>::second': member could not be initialized
how could i figure out where the problem is? the only place i use a pair with 'IntersectionData*' is here:
#include "MRMaterialMatth.h"
#include "IntersectionData.h"
using namespace std;
struct IShaderMatth {
virtual ~IShaderMatth() {}
vector<pair<MaterialMatth,IntersectionData*> > traceCols;
};
and there are not any other compiler errors
how can I track down this?
//edit: utility is not my code. it must be from std.. the code of line 49 looks like this:
template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}
line 49 is the line of the comment
edit2:
the only places where i change something about the content of tracecols look like this:
IntersectionData* iDataOut = NULL;
if(newIData_out!=NULL)
{
iDataOut = new IntersectionData(*iData);
}
traceCols->push_back(make_pair(MaterialMatth(),iDataOut));
and
if(traceCols){
traceCols->push_back(make_pair(MaterialMatth(), NULL));
}
and
if(traceCols)
{
(*traceCols)[traceCols->size()].second = new IntersectionData(*newIData);
}
is NULL the problem? it's a pointer, so i should be allowed to create a pair with NULL, no??
Try explicitly casting the NULL to IntersectionData * in your call to make_pair().
if(traceCols){
traceCols->push_back(make_pair(MaterialMatth(), (IntersectionData *)NULL));
}
There is a problem initializing one of those pairs.
Ask yourself, "What initializes that?"
The answer is the vector traceCols.
Now ask, "Where am I creating elements in traceCols?"
Once you answer that, you should know what is going wrong.
Watch out for the line (*traceCols)[traceCols->size()].second = new IntersectionData(*newIData) - it seems like that would go out of the vector's bounds (since the largest index of a vector is size() - 1).
I'm not sure if the NULL is causing it - so comment out that line, and see for yourself (or try Dave's suggestion)! If it doesn't work, comment out another. Eventually, you'll either find what line, and be able to ask a more specific question, or it'll be none of those things, and you'll know you have to search somewhere else. That's what I do when I see all these silly compiler error messages.
It looks like you have an assignment somewhere from a pair<MaterialMatth,int>. The compiler is trying to convert from that to the declaration you listed, but it can't convert from an int to a pointer without an explicit cast.