I cannot initiate an integer vector in C++ [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have the following code excerpt.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <string>
#include <array>
using namespace std;
int solver(int T)
{
/* read IA */
ifstream inputFile("IA [0;1.3077].txt");
vector<int> ia;
if (inputFile) {
int num;
while ( inputFile >> num) {
ia.push_back(num);
}
}
}
int main (void) {
solver(360);
}
But it gives me this error:
error: implicit instantiation of undefined template
'std::__1::vector<int, std::__1::allocator<int> >'
vector<int> ia;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:200:29: note:
template is declared here
class _LIBCPP_TYPE_VIS_ONLY vector;
The goal is to read a txt file with integers per line without knowing how many lines there are in advance. I'm choosing a vector to hold the data because I don't want to initialize an integer array with a fixed size. Does anybody have any suggestions?
Also, I understand that the variable T is unused - I will use it after the .txt file is loaded.

You need to:
#include <vector>
You must alway include directly all the headers for the types you use.

Related

C++ use of undeclared identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I know there's a lot of different threads with this similar issue but I've gone through a number of them and have fixed any of the possible issues that may be resulting in this error but I keep getting it.
I'm trying to test some of functions in my class file but whenever I try calling them I get that error.
Here's one for example.
main.cpp
#include <iostream>
#include <vector>
#include "Graph.h"
using namespace std;
int main(){
Graph graph;
newState();
return 0;
}
Graph.h (I know I shouldn't be using namespace here but I'm still doing it for now)
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class Graph{
public:
Graph();
Graph(vector<Edge> const &edges, int N);
void printGraph(Graph const &graph, int N);
// print adjacency list representation of graph
void createTree(GraphNode *root, int state[]);
void newState();
private:
int states = 8;
int initialState[8];
int nextState[8];
};
Graph.cpp
#include "Graph.h"
void Graph::newState(){
srand (time(NULL));
int random = rand() % 4 + 1;
cout << random;
};
I feel like I'm trying to do the bare minimum but it just does not want to work. Can anyone please tell me where I'm going wrong?
newState is not a free-standing function. It is a non-static member function of Graph class. Which means, it must be called on an instance of Graph. What you probably meant to do is:
Graph graph;
graph.newState();

Error: invalid use of 'void' (using vectors) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to create a code using vectors and other c++11 utilities. The above mentioned(on the title) error occurs in my code and despite I looked for a solution to this error on the internet I did not find something that works into my code. I tried to make some type castings but did not work. I present you the contentious part of code below:
#include <iostream>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <algorithm>
#include <list>
//#include <Winbase.h>
using namespace std;
// A struct describing a product.
typedef struct Products
{
string category;
string name;
float price;
} Product;
inline void scenario1(int num_cashiers)
{
vector<Product> products; // It is a vector(a pseudo-second dimension) of products which will be used for each customer
vector<vector<Product>> customers; // A vector containing all customers
vector<vector<vector<Product>>> cashiers(num_cashiers); // A vector describing the supermarket cashiers declaring a queue of customers for each cashier
double start = GetTickCount(); // It will be used for counting 10 secs until next update
vector<int> total_products(num_cashiers); // A vector keeping the total number of products of each queue
list<string> categories; // A list containing all the categories of the products
list<float> categories_prices(categories.unique().size()); // A list containing all category prices
//THE ABOVE LINE - THE LAST ONE IN THIS PART OF CODE - IS THE LINE I GET THE ERROR!!!
....
}
What is wrong with the code?
Thank you everyone in advance!
list::unique is a void function, it does not return anything. On that last line, where you call categories.unique().size(), you are calling .size() on a void expression.

"ISO C++ forbids declaration of 'list' with no type" error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I keep getting the following errors on every line containing list<string> below:
ISO C++ forbids declaration of 'list' with no type
expected ';' before '<' token
#ifndef __REGNAMEGENERATOR_H
#define __REGNAMEGENERATOR_H
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#define Max_reg_Num 1000
using namespace std;
class RegNameGenerator{
private:
int intRegNumber;
int realRegNumber;
list<string> UsedIntReg; // error
list<string> UsedRealReg; // error
public:
RegNameGenerator();
~RegNameGenerator();
string generateIntReg();
string generateRealReg();
list <string> getUsedIntReg(); // error
list <string> getUsedRealReg(); // error
int getIntRegNum();
int getRealRegNum();
};
#endif
You have to include header <list>:
#include <list>

main.cpp|45|error: expected primary-expression before "int" and "double" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
File sys.cpp:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <fstream>
using namespace std;
[...]
struct kmph_in_mps
{
int kmph[4];
int result[4];
void kmph_erfassen()
{
for (size_t i = 0; i < 4; ++i)
{
cin >> kmph[i];
}
}
void mps_erfassen(int kmph, double result)
{
result = kmph / 3.6;
}
void ergebniss_ausgeben()
{
cout << endl << kmph << "Km/h sind " <<result << " Meter pro Sekunde\n";
}
};
[...]
File main.cpp:
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include "sys.cpp"
#include <fstream>
kmph_in_mps c;
[...]
void Kmph_in_mps()
{
system("cls");
cout << "\nKm/h: ";
c.kmph_erfassen();
c.mps_erfassen(int kmph, double result);
c.ergebniss_ausgeben();
t.beenden();
}
[...]
Errors:
Error: expected primary-expression before “int”
Error: expected primary-expression before “double”
I am a learning C++, and I dont get what to do now.
I am searching for answers at the internet and here, but I dont realy find the right one.
Which primary expression do I have to write before "in" and "double"?
Or am I doing everything completly wrong, like a beinner does? :P
Edits:
I tryed return result, but it seems to be not allowed in a void.
I already tryed c.mps_erfassen(); but it is gives me an error, too: error: no matching function for call to `kmph_in_mps::mps_erfassen()'|
I already tryed c.mps_erfassen(kmph, result); but then I do not declare them both in this scope. They are declared in the other file (sys.cpp). :S
c.mps_erfassen(int kmph, double result);
//^^^remove int and double
When you call a function, you should not put the type before the parameters.
This
c.mps_erfassen(int kmph, double result);
should be
c.mps_erfassen(kmph, result);
Let function deduce the type :)
EDITED IN RESPONSE TO COMMENT:-
You are creating object of struct in main.cpp while it's definition is in sys.cpp. How would main.cpp would get to know what your struct means.
For better design, place declaration of struct in header file say sys.h, then define required members in .cpp file say sys.cpp ( you have to include sys.h ). And then use that struct in your main.cpp ( again you have to include sys.h over here ).

programme crashes when trying to cout map value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to insert pair values < float,string > into my map class
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <set>
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<float,string> output;
output.insert(pair<float,string> ( 200.5, "foo" ));
output.insert(pair<float,string> ( 100.5, "batr" ));
map<float,string>::iterator mps1;
map<float,string>::iterator mps2;
mps1 = output.begin();
mps2 = output.end();
while (mps1 != mps2)
{
cout<<mps2->first
<<" "
<<mps2->second; //crashes here
mps1++;
}
system("PAUSE");
}
Using the debugger , it crashes when it does to the following line
<<mps2->second;
Can someone explain to me , thanks
You're supposed to access mps1, not mps2.
mps1 is the iterator you're incrementing for use; mps2 is the "end iterator" which you must not dereference.
It's a pretty basic typo / logic error.