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.
Related
I wrote the following code :
#include<bits/stdc++.h>
using namespace std;
int lengthOfLastWord(string A) {
int m= A.size();
int i=0;
while(i<m)
{
if(A[i]==' ')
{
int count=0;
i++;
while(A[i]!=' ')
{
count++;
i++;
}
}
else
i++;
}
return count;
}
int main()
{
string A;
getline(cin,A);
cout<<lengthOfLastWord(A);
}
But it is showing the following error
error: cannot resolve overloaded function ‘count’ based on conversion to type ‘int’
I am unable to understand why is it showing this error and what should I do to fix it.
Please help me.
Thankyou
Inside the if scope, your count variable is colliding with (technically "shadowing") std::count. However, your local count does not exist in the scope of your return statement, so the compiler is trying to use the only count that it knows about at that point, which is std::count.
This is a great example of why using namespace std and #include<bits/stdc++.h> are both bad ideas. If proper includes and namespaces were used, this code would have given you a much clearer compile error:
error: 'count' was not declared in this scope
The most direct reason is because your count variable is defined in the if scope, and not available in the scope of return statement.
However, the error you are seeing is confusing, since you have using namespace std in your code, and it makes completely unrelated (for your purposes) function std::count visible everywhere in your program, including your return statement. From the compiler perspective, you are trying to return a pointer to std::count, but since this function is overloaded template, compiler doesn't know which one you are trying to return - thus the phrasing of the errror.
If you remove the using namespace std from your code, which you should have never had in the first place, your code will still fail to compile, but error message would be way more easier to understand.
One solution is to rename your count variable to something else like total.
The compiler is considering std::count function instead of the count variable which is int.
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
I tried to find this but can't find any. I know I can create a reference to an array variable:
int x[10] = {}; int (&y)[10] = x;
However, in the case that the array size is not known at compile time, like in the following code:
const int n = atoi( string ); //the string is read from a text file at run time.
int x[n] = {}; int (&y)[n] = x; //this generates a compiling error.
Even if int n is declared const, as long as n is not known at compile time, the reference is invalid. The compiler will say something like this: reference to type 'int [n]' cannot bind to a value of unrelated type 'int [n]'. Anyone has any idea about how to fix this? Thanks in advance.
Runtime-length arrays are a C99 feature and do not exist in standard C++. They're present as an extension on some C++ compilers, but don't mix well with C++ features, like references and templates.
You should probably use a vector.
The feature of declaring arrays dynamically like shouldn't be used in C++. Not all compilers support it. Consider using the STL containers instead. Like std::vector<int>
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>.
I've got some problems/misunderstandings with arrays in C++.
int myArray[30];
myArray[1]=2;
myArray[2]=4;
This is spitting out a lot of compiler errors. I don't think it is necessary to include them here as this is an easy question for everybody with experience in C(++) I guess
Why doesn't this work?
Is there a way to create a "dynamic" array that has a fixed ammount of values (so no malloc needed) but where I can change the values during runtime?
I'm guessing you have that outside of a function.
You are allowed to define variables outside of a function. You can even call arbitrary code outside of a function provided it is part of a variable definition.
// legal outside of a function
int myArray[30];
int x = arbitrary_code();
void foo()
{
}
But you cannot have arbitrary statements or expressions outside of a function.
// ILLEGAL outside a function
myArray[1] = 5;
void foo()
{
// But legal inside a function
myArray[2] = 10;
}
Are you saying that this doesn't compile:
int main() {
int myArray[30];
myArray[1]=2;
myArray[2]=4;
}
If it doesn't, you have something wrong with your compiler setup. As I said in my comment, we need to see the error messages.