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.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I don't know what's wrong here ?
It's just running errors !!!
#include <iostream>
using namespace std;
int main()
{
cout << string("hello world");
return 0;
}
Read more about C++. So read first Programming -- Principles and Practice Using C++.
Then read C++ reference documentation, notably the one about std::string-s.
You need to #include <string>
You should enable all warnings when compiling. If using GCC, compile with g++ -Wall -g
You don't need that string before the actual string:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
return 0;
}
Or, alternatively, if you're trying to store a string:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "hello world";
cout << str;
return 0;
}
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.
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'm really really new to C++ and this is my first program on Visual Studio 2015, It shows me 2 errors:
"{" missing function header (old style format list)
expected a declaration
#include "stdafx.h"
#include <iostream>
int main();
{
cout << "Hello World";
return 0;
}
int main() remove ; at the end.
#include "stdafx.h"
#include <iostream>
int main(); // this ';' is giving problem remove it.
{
std::cout << "Hello World"; // use std::cout
return 0;
}
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
#ifndef _SOLDIER_H_
#define _SOLDIER_H_
#include <iostream>
#include <stack>
using namespace std;
class Soldier {
// your code here
private:
stack<int> list;
public:
Soldier(int num);
bool check(vector<int> arrange);
};
#endif
Error is at the above Soldier.h, at bool check(vector arrange);
15 C:\Users\king boon\Desktop\CS1020E\lab4\lab4\ex1\skeleton\Soldier.h expected `;' before '(' token
#include "Soldier.h"
// your code here
Soldier::Soldier(int num) {
int i;
for (i=1; i<=num; i++) {
list.push(i);
}
};
bool Soldier::check(vector<int> arrange) {
return true;
};
Been trying for hours, at my wits end. Thanks.
vector isn't declared:
#include <vector>