Simple question....
I have a header file called bag...
#include <map>
#include <iostream>
#include <string>
using namespace std;
class Bag
{
public:
Bag();
map <const string str, int number> items;
private:
};
#endif
In the implementation, I'd like to insert something into bag:
#include <string>
#include <fstream>
#include <map>
#include "Bag.h"
using namespace std;
Bag::Bag()
{
items["brush"] = 4;
}
But for some reason, i can't access items. What am I doing wrong????
Thanks!
Duh! For some reason I'm trying to insert a value name with the value type field. Thanks Chad, I think you fixed me!
map <const string, int> items;
Related
I am trying to use boost::container::map. During inserting data, the error "insert is ambiguous" is shown.
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert("Ram",0);
}
Your style of inserting is not correct. I provide the code:
#include <boost/container/map.hpp>
#include <string>
#include <iostream>
#include <ostream>
int main()
{
boost::container::map<std::string, int> map;
map.insert(std::pair<const std::string, int>("Ram",1));
std::cout<< map["Ram"];
return 0;
}
I'm getting an error everytime I compile the function.cpp file saying that stocks and newStock are not declared in this scope. I'm trying to use a struct inside a vector. Thanks for the help.
This is the main.cpp file
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
struct Stocks
{
int one;
int two;
int three;
};
vector<Stocks> portfolio;
#include "testProject2.h"
int main()
{
buyStock(portfolio);
}
This is the header file.
#include <iostream>
void buyStock(vector<Stocks>& Portfolios);
This is the function.cpp file
#include <iostream>
#include <vector>
#include "testProject2.h"
void buyStock(vector<Stocks>& Portfolios)
{
Stocks newStock;
newStock{1,2,3};
Portfolios.push_back(newStock);
}
Your function.cpp file has no way to know what the Stocks struct is. Define it in the header file:
struct Stocks {
int one;
int two;
int three;
};
And remove its definition from main.cpp.
Also in your header file, you need
#include <vector>
and refer to vector parameter as std::vector<Stocks> &Portfolios (better than using namespace std;)
Your initialization syntax newstock{1,2,3} looks incorrect too.
You use vector in your header file without it being defined.
Try changing the header file to this:
#include <vector>
#include <Stocks.h> // name of .h file where Stocks is defined
void buyStock(std::vector<Stocks>& Portfolios);
// OR
using namespace std::vector;
void buyStock(vector<Stocks>& Portfolios);
I want to fetch value of certain key from back-end.
In back-end the structures are defined and values are initialized.
Consider this as structure defined in back-end:
struct person{
string nme;
string adrs;
int id;
};
person p1 = {"steve","ABC street",23};
The key address corresponds to value of p1.adrs in back-end.
Now the key address is to be mapped to (p1 & adrs) in external file and should get the value "ABC street".
My question is how mapping is to be done for key and its particular structure member in external file and how to fetch value for that key.
Here Can I use std::map concept for this?
I got solution for requirement. But to improve it further, mapping is to be declared in separate file (Here it was declared in .h file)
myfile.h
#ifndef MYFILE_H
#define MYFILE_H
#include <iostream>
#include <algorithm>
#include<vector>
#include <string>
#include <map>
using namespace std;
struct Chassis
{
string InLED;
string AstTg;
};
struct Manager
{
string MngrType;
int count;
};
const Chassis chassis1={"On","null"};
const Manager manager1={"BMC",23};
const map<string, const string>cha1={{"IndicatorLED", chassis1.InLED},{"AssetTag",chassis1.AstTg},{"ManagerType",manager1.MngrType}};
const map<string, int>cha2={{"Count",manager1.count}};
void func(string);
#endif
myfile.cpp
#include <iostream>
#include <string>
#include "myfile.h"
#include <map>
using namespace std;
void func(string item)
{ if(cha1.find(item) == cha1.end()) {if (cha2.find(item) == cha2.end()){} else {cout<< item<<":"<<cha2.at(item)<<endl;} }
else {cout<< item<<":"<<cha1.at(item)<<endl;}
}
main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "myfile.h"
int main() {
string item="IndicatorLED";
func (item);
string item1="AssetTag";
func(item1);
string item2="ManagerType";
func(item2);
string item3="Count";
func(item3);
}
i am trying to find the common elements(song_list) between two vectors, in my user.h file i have two vectors and am trying to use this vectors in another class(help.cpp) but i cant get it right. please whats wrong with my code? it doesn't run and am not entirely sure if this is the right approach.
user.h
#pragma once
#include <string>
#include <vector>
#include <string>
using namespace std;
class User
{
public:
User(void);
~User(void);
public:
struct user1
{
string name;
int age;
string song_list;
};
typedef vector <user1> u1;
struct user2
{
string name;
int age;
string song_list;
};
typedef vector <user2> u2;
};
help.cpp
#include "Help.h"
#include "User.h"
#include <iterator>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
int commonElements();
int commonElements(){
vector <user1> u1;
vector <user2> u2;
std::sort(u1.begin(), u1.end());
std::sort(u2.begin(), u2.end());
std::vector<string> common;
std::set_intersection(u1.begin(), u1.end(), u2.begin(), u2.end(),
std::back_inserter(common));
cout<<common<<endl;
}
thats my code so far. All data for the user's name,age and song list is on a .txt file.
You seem to be somewhat confused about types vs. instances. You're creating some separate types where (I'm pretty sure) you just want two instances. For example, it appears (to me) that you really want u1 and u2 to be vectors of the same type of objects.
user.h:
#pragma once
#include <string>
#include <vector>
#include <string>
using namespace std;
struct user {
string name;
int age;
vector<string> song_list;
};
help.cpp:
#include "Help.h"
#include "User.h"
#include <iterator>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
int commonElements(user &u1, user &u2) {
sort(u1.song_list.begin(), u1.song_list.end());
sort(u2.song_list.begin(), u2.song_list.end());
vector<string> common;
set_intersection(u1.song_list.begin(), u1.song_list.end(),
u2.song_list.begin(), u2.song_list.end(),
back_inserter(common));
for (auto const &song : common)
cout << song << "\n";
#if 0
// alternatively, write the intersection directly to the stream:
set_intersection(u1.song_list.begin(), u1.song_list.end(),
u2.song_list.begin(), u2.song_list.end(),
ostream_iterator<string>(cout, "\n"));
#endif
}
As an aside, it's generally agreed that putting a using namespace std; in a header is a really lousy idea. I've left it because it's mostly unrelated to the question(s) at hand, but it should really be changed.
So in my cpp file I'm trying to declare a map as follows:
map<string, vector<myStruct>> myMap;
At the top of my file I have written using namespace std and I also have #include <string>
.
However I'm getting these weird errors:
error: ISO C++ forbids declaration of ‘map’ with no type
I don't know how to fix it. If I write #include <map> that just causes the compiler to freak out.
do you have #include <map>? rest looks valid,
however you might need to add a space if your C++ standard is not C++11:
#include <map>
#include <vector>
#include <string>
using namespace std;
map<string, vector<myStruct> > myMap;
^^^
even better not use namespace std:
#include <map>
#include <vector>
#include <string>
std::map<std::string, std::vector<myStruct> > myMap;
You need to include map header file.
#include <map>
Meanwhile, in case you are not using C++11, you need a space:
map<string, vector<myStruct> > myMap;
//^^
You should also include <map>. std::map is introduced through this header.
Furthermore, using namespace std is considered a bad practice. You should either have a using statement or use the prefix the name with std:: to denote a fully-qualified identifier:
#include <map>
#include <string>
#include <vector>
std::map<std::string, std::vector<myStruct>> myMap;
Note, the lack of a using statement ;)
#include <vector>
#include <string>
#include <map>
#include <iostream>
typedef int myStruct;
std::map<std::string, std::vector<myStruct>> myMap;
int
main()
{
std::vector<myStruct> testMe = { 1, 2, 3};
myMap["myTest"] = testMe;
std::cout << myMap.size() << std::endl;
return(0);
}