I am getting below error:
2 IntelliSense: function "std::basic_ifstream<_Elem, _Traits>::basic_ifstream(const std::basic_ifstream<_Elem, _Traits>::_Myt &) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 818 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\fstream") cannot be referenced -- it is a deleted function c:\Users\varunk\Documents\Visual Studio 2013\Projects\hashmaptest\hashmaptest\hashmaptest.cpp 12 48 hashmaptest
Here is my code which is showing the error:
// hashmaptest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Init ini;
ifstream file("D:/test.txt", ios::in);
ini.filterng_three_addId_on_location_and_time(file);
file.close();
// Node shoom = Node ("ram", 14);
// myMap.insert(std::make_pair(Node("abc",18), 16));
return 0;
}
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include "stdafx.h"
#include <iostream>
#include <unordered_map>
#include <string>
#include<vector>
#include<string>
#include <bitset>
#include <utility>
#include <fstream>
using namespace std;
class FilterKeys
{
public:
FilterKeys(string a, int b)
{
name = a;
time = b;
}
string name;
int time;
};
class HashKey
{
public:
size_t operator() (FilterKeys const& key) const
{
return std::hash<std::string>()(key.name) ^
(std::hash<int>()(key.time) << 1);
}
};
class EqualTo
{
public :
bool operator()(const FilterKeys& lhs, const FilterKeys& rhs) const
{
return lhs.name == rhs.name && lhs.time == rhs.time;
}
};
class Init {
unordered_multimap<FilterKeys, int, HashKey, EqualTo> _myMap;
public:
void filterng_three_addId_on_location_and_time(ifstream file)
{
string location;
int time, addId;
if (!file)
cerr << "Cant open " << endl;
while (file >> location >> time >> addId)
{
if (_myMap.count(FilterKeys(location, time)) < 3)
_myMap.insert(std::make_pair(FilterKeys(location, time), addId));
}
}
unordered_multimap<FilterKeys, int, HashKey, EqualTo> getHashMap()
{
return _myMap;
}
};
The error is telling you that you can't call the copy constructor of ifstream - it doesn't have one.
You need to change:
void filterng_three_addId_on_location_and_time(ifstream file)
to
void filterng_three_addId_on_location_and_time(ifstream& file)
This will mean that you pass a reference to the file object (rather than a copy). You need to be aware that passing a reference means that changes you make in the called function will be seen in main.
In other cases, you may want to pass a reference, but forbid the called function from making changes to the object. In that case you would write:
void filterng_three_addId_on_location_and_time(const ifstream& file)
(a reference to const). That won't work here, because all the input functions modify the ifstream object, so you would get a compiler error when you called them.
Related
I'm creating a student data management console application for a project. I created a class called Student which is storing all the data that a student needs to have, and it also has all the getters and setters associated with it. Here is how all my files are laid out:
Student.h
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string id;
string email;
int presentation;
int essay1;
int essay2;
int project;
public:
//constructor
//Student();
//setters
void set_name(string);
void set_id(string);
void set_email(string);
void set_presentation(int);
void set_essay1(int);
void set_essay2(int);
void set_project(int);
//getters
string get_name();
string get_id();
string get_email();
int get_presentation();
int get_essay1();
int get_essay2();
int get_project();
};
Student.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
//constructor definition
/*
Student::Student(void) {
cout << "Student created" << endl;
}
*/
//setter definition
void Student::set_name(string s) {
name = s;
}
void Student::set_id(string s) {
id = s;
}
void Student::set_email(string s) {
email = s;
}
void Student::set_presentation(int a) {
presentation = a;
}
void Student::set_essay1(int a) {
essay1 = a;
}
void Student::set_essay2(int a) {
essay2 = a;
}
void Student::set_project(int a) {
project = a;
}
//getter definition
string Student::get_name() {
return name;
}
string Student::get_id() {
return id;
}
string Student::get_email() {
return email;
}
int Student::get_presentation() {
return presentation;
}
int Student::get_essay1() {
return essay1;
}
int Student::get_essay2() {
return essay2;
}
int Student::get_project() {
return project;
}
Main.cpp
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main() {
cout << "Hello World!" << endl;
Student student1;
Student student2;
Student student3;
student1.set_name("John");
student2.set_name("Bob");
student3.set_name("Carl");
return 0;
}
When I try to run my program, I get, amongst others, the following errors:
Error 1 error C2011: 'Student' : 'class' type redefinition
Error 2 error C2079: 'student1' uses undefined class 'Student'
Error 5 error C2228: left of '.set_name' must have class/struct/union
Error 9 error C2027: use of undefined type 'Student'
How can I go about fixing this issue?
I'm quite sure this is an error caused by the fact that student.h is included twice in a certain .cpp file. Thus you need to use so-called header guards to make sure the file is only included once in every .cpp file:
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
/* ... */
};
#endif
The idea behind this is that an #include is a preprocessor directive that results in the argument file being copied into the file where the #include was issued. Hence, if files A and B include Student.h, and file C includes both files A and B, then the declaration of class Student is going to end up duplicated. Hence the error. The above macros make sure that this doesn't happen.
Edit as per the question author's comment:
#pragma once is the same as #ifndef .. #define #endif but non-standard .
See #pragma once vs include guards? for reference.
I had the same error. I just clean and rebuild the solution and error resolved.
I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.
I'm fairly new to C++ and i'm trying to build a linked list with a container class called FlexString. In main() I want to instantiate the FlexString class by simply saying: "FlexString flex_str = new FlexString();" calling the constructor etc. But it won't compile, the error is at the bottom. Here is my code:
//FlexString.h file
#ifndef FLEXSTRING_CAMERON_H
#define FLEXSTRING_CAMERON_H
#include "LinkedList.h"
#include <string>
using namespace std;
using oreilly_A1::LinkedList;
namespace oreilly_A1 {
class FlexString {
public:
FlexString();
void store(std::string& s);
size_t length();
bool empty();
std::string value();
size_t count();
private:
LinkedList data_list;
};
}
#endif
Here is the .cpp file for the FlexString class:
#include "FlexString.h"
#include "LinkedList.h"
#include <string>
using namespace std;
namespace oreilly_A1 {
FlexString::FlexString() {
}
void FlexString::store(string& s) {
data_list.list_head_insert(s);
}
std::string value() {
data_list.list_getstring();
}
}
Here's the main program file.
#include <iostream>
#include <cstdlib>
#include "FlexString.h"
using namespace std;
using oreilly_A1::FlexString;
int main() {
FlexString flex_str = new FlexString();
cout << "Please enter a word: " << endl;
string new_string;
cin >> new_string;
flex_str.store(new_string);
cout << "The word you stored was: "+ flex_str.value() << endl;
}
error: conversion from 'oreilly_A1::FlexString*' to non-scalar type 'oreilly_A1::FlexString' requested. "FlexString flex_str = new FlexString();"
FlexString flex_str = new FlexString();
is wrong since the RHS of the assignment is a pointer to a FlexString while the LHS is an object.
You can use:
// Use the default constructor to construct an object using memory
// from the stack.
FlexString flex_str;
or
// Use the default constructor to construct an object using memory
// from the free store.
FlexString* flex_str = new FlexString();
I'm a little confused by this error that I'm having (I'm using VS2012).
Here's my code:
RecipeBook.h:
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;
class RecipeBook
{
private:
vector<SingleRecipe> *recipe;
SingleRecipe *one;
public:
RecipeBook(vector<SingleRecipe> *recipe);
void addRecipe(SingleRecipe *one);
bool removeRecipe(string name);
vector <SingleRecipe> *returnListOfRecipes(double time);
};
#endif
SingleRecipe.h:
#ifndef SINGLERECIPE_H
#define SINGLERECIPE_H
#include <string>
#include <vector>
using namespace std;
class SingleRecipe
{
private:
string name;
vector<string> ingredients;
vector<string> method;
int numOfServing;
double time;
public:
SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time);
string getName();
void setName();
int getNumOfServing();
void setNumOfServing();
double getTime();
void setTime();
string toString();
};
#endif
BookAndRecipe.cpp:
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;
vector <SingleRecipe> *RecipeBook::returnListOfRecipes(double time)
{
vector<SingleRecipe> *two;
for (int i = 0; i = recipe->size(); i++)
{
if (recipe[i].data()->getTime < time)
{
*two->push_back(recipe[i].pop_back());
}
}
return NULL;
}
Over at returnListOfRecipes() I get this error:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=SingleRecipe, _Alloc=std::allocator<SingleRecipe>]" matches the argument list
argument types are: (void)
object type is: std::vector<SingleRecipe, std::allocator<SingleRecipe>> c:\Users\Ventus\Documents\Visual Studio 2012\Projects\Recipe\Recipe\BookAndRecipe.cpp 83 8
I suspect it might have something wrong with my for loop, but I'm not very experienced, so I might be doing something very wrong here.
I appreciate all help that's given!
pop_back() doesn't return a value, it just drops the last element of the container. You probably want:
*two->push_back(recipe[i].back());
recipe[i].pop_back();
Friend functions can't access variables of the classes
I'm having a problem with several friend functions not being able to access the variables in classes where they have been declared as friends.
The actual error text is:
error: 'fid' was not declared in this scope. this repeats for the other private variables.
The same error is given for three functions, read, negative, and write.
A couple of notes:
1) This lab requires that I write the code so that the functions can be used by both classes.
I'm compiling this in windows with code::blocks using g++ and I've also tried compiling my code in ubuntu using g++ from the terminal using the -g flag and I get the same error both times.
Any suggestions you have would be greatly appreciated.
Header File
#ifndef PXMUTILS_H
#define PXMUTILS_H
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef unsigned char uchar;
class pgm
{
public:
pgm();
~pgm();
void read(string &);
void negative();
void write(string);
friend void read (const string &);
friend void write(string);
friend void negative();
private:
int nr;
int nc;
int mval;
int ftyp;
string fid;
uchar **img;
};
class ppm
{
public:
ppm();
~ppm();
void read(string &);
void negative();
void write(string);
friend void read (const string &);
friend void write (string);
friend void negative ();
private:
int nr;
int nc;
int mval;
int ftyp;
string fid;
uchar **img;
};
#endif
C++ program
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include "pxmutils.h"
using namespace std;
typedef unsigned char uchar;
uchar ** newimg(int nr, int nc, int ftyp)
{
uchar **img=new uchar *[nr];
img[0]=new uchar [nr*nc*ftyp];
for(int i=1; i<nr; i++)
{
img[i]=img[i-1]+nc*ftyp;
}
return img;
}
void deleteimg(uchar **img)
{
if(img)
{
if(img[0])
{
delete [] img[0];
}
delete [] img;
}
}
void read (const string &fname)
{
ifstream fin(fname.c_str(), ios::in);
if(!fin.is_open())
{
cerr<<"Could not open "<<fname<<endl;
exit(0);
}
fin >>fid
>>nc
>>nr
>>mval;
while (fin.get() != '\n') { /*skip to EOL */ }
img=newimg(nr, nc);
fin.read((char *)img[0], nr*nc);
fin.close();
}
void set_cmap(string mname)
{
}
void negative()
{
for(int i=0; i<nr; i++)
{
for(int j=0; j<nc; j++)
{
int t=img[i][j];
img[i][j]=(255-t);
}
}
}
void write(string fname)
{
ofstream fout (fname.c_str(), ios::out);
size_t dp;
if ((dp = fname.rfind(".pgm")) != string::npos)
{
fout<<"P5"<<endl;
}
if((dp= fname.rfind(".ppm")) != string::npos)
{
fout<<"P6"<<endl;
}
fout<<nc<<" "<<nr<<endl;
fout<<mval<<endl;
for(int i=0; i <nr; i++)
{
for (int j=0; j<nc; j++)
{
fout<<img[i][j]<<" ";
}
fout<<endl;
}
fout.close();
}
pgm::pgm()
{
nr=0;
nc=0;
mval=0;
ftyp=1;
fid="";
img=NULL;
}
pgm::~pgm()
{
deleteimg(img);
}
ppm::ppm()
{
nr=0;
nc=0;
mval=0;
ftyp=1;
fid="";
img=NULL;
}
ppm::~ppm()
{
deleteimg(img);
}
Program to test functions
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#include "pxmutils.h"
int main(int argc, char *argv[])
{
if (argc == 1) {
cerr << "No input file specified!\n";
exit(0);
}
string fname = argv[1];
size_t dp;
if ((dp = fname.rfind(".pgm")) == string::npos) {
cout << "PGM error: file suffix " << fname
<< " not recognized\n";
exit(0);
}
fname.erase(dp);
pgm img_g;
ppm img_c;
img_g.read(fname+".pgm");
if (argc == 3)
img_c.set_cmap(argv[2]);
img_c = img_g;
img_g.negative();
img_g.write(fname+"_n.pgm");
img_c.write(fname+"_c.ppm");
}
fin >>fid
>>nc
>>nr
>>mval;
while (fin.get() != '\n') { /*skip to EOL */ }
In this code, fid, nc, nr etc are undefined. You need to use the class instance to be able to access them, they don't exist by themselves.
Your functions don't accept the class objects as parameters, so how are you going to read into them?
You should have another think of your design. It is best to avoid friend functions if possible,
You need to go a bit back to basics. When you define non-static members of a class you are defining attributes or operations of the objects of the class, but those attributes don't exist by themselves, only as part of the instances of the class.
This concept is orthogonal to access and access specifiers, that is, this is so regardless of the members being public, protected or private. Once you have an instance, when your try to access those members the access specifiers come into play, and there is where friendship comes into play: it will grant your code access to members that would otherwise be inaccessible (private or protected outside of the inheritance hierarchy).
The problem in your code is that you don't have an object, and thus cannot access the members of the object. You will need to either create or pass an object of the appropriate type to the functions.
There are other problems in the code, like for example, the memory allocations inside newimg look a little suspicious (what were you intending to allocate?) but that is outside of the scope of this question.