im getting the above error for the following code. Please help me with what i am doing wrong. This code has nested structures. For now it generates name for the generation structure based on the input given by the user.
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <limits>
#include <stdio.h>
#include <sstream>
using namespace std;
using std::stringstream;
struct rootset {
double totSize;
const char *rStrtPtr;
const char *rEndPtr;
struct generations {
double totSize;
const char *genStrtPtr;
const char *genEndPtr;
int numOfGen;
int genName;
struct object {
double objSize;
const char *objStrtPtr;
const char *objEndPtr;
string id;
char markBit;
char objPtr;
struct freeList {
double freeSpace;
int flNumb;
};
};
} generation;
};
int main()
{
int gen =0;
cin >> gen;
rootset* pRootSet = (rootset*)malloc(1200);
for( i=0; i<gen; i++) {
stringstream out;
out << i;
string s = out.str();
string foo = "generation";
pRootSet->generation.genName = foo.append(s); /*Error is here*/
cout<<"foo: "<<foo<<endl;
}
}
i am trying to print this answer:
4
foo: generation0
foo: generation1
foo: generation2
foo: generation3
generation.genName is of INT type while you want to pass a string value without any conversion, that's why it gives it an error. Solution: do the conversion before.
Related
I am trying to create a function that returns in main.cpp in the header and .cpp file and run it in the main function.
This process I do works on main.
#include <iostream>
#include <sstream>
#include "Cards.h"
using namespace std;
//this function returns array
int *function1(){
int a=12;
int b=13;
int c=14;
static int list[3]={a,b,c};
return list;
}
int main(int argc, const char * argv[]) {
int *list;
list=function1();
cout<<list[1]<<endl;
return 0;
}
However, I cannot do these in a header and a separate cpp file.
I have a Cards header
#ifndef Cards_H
#define Cards_H
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Cards{
public:
char suit; //A,H,D,C,S. A is empty card
int number; //00-13
int visibilty;//0 - 1. O invisible 1 is visible
int * function2();
};
#endif
This is the class cpp file
#include "Cards.h"
using namespace std;
//function
int Cards:: function2(){
int a=12;
int b=13;
int c=14;
int list[3]={a,b,c};
return list; // error code Cannot initialize return object of type 'int Cards::*' with an lvalue of type 'int [3]'
}
How do I fix this problem and run it in main?
As pointed out in the comments, there is already a SO thread
Return array in a function
which handles your issue.
If your really want to use C arrays then your program shall look like:
Cards_CStyle.h:
#ifndef Cards_CStyle_H
#define Cards_CStyle_H
using namespace std;
class Cards {
public:
int* function2(int arr[]);
};
#endif
Cards_CStyle.cpp:
#include "Cards_CStyle.h"
using namespace std;
//function
int* Cards::function2(int arr[]){
int a=12, b=13, c=14;
arr[0] = a;
arr[1] = b;
arr[2] = c;
return arr;
}
main_CStyle.cpp:
#include <iostream>
#include "Cards_CStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
int arr[3]; // Take care that all your functions use size <= 3
Cards cards;
int* list=cards.function2(arr);
cout<<list[1]<<endl;
return 0;
}
As recommended in the comments, you should use the containers of the STL, e.g. array for fixed length or vector for variable length. Assuming that fixed length of 3 will be fine for you, then your code would be looking like this:
Cards_STLStyle.h:
#ifndef Cards_STLStyle_H
#define Cards_STLStyle_H
#include<array>
using namespace std;
typedef array<int, 3> my_array;
class Cards {
public:
my_array function2();
};
#endif
Cards_STLStyle.cpp:
#include "Cards_STLStyle.h"
using namespace std;
//function
my_array Cards::function2(){
int a=12, b=13, c=14;
return my_array { a,b,c};
}
main_STLStyle.cpp:
#include <iostream>
#include <array>
#include "Cards_STLStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
Cards cards;
my_array list=cards.function2();
cout<<list[1]<<endl;
return 0;
}
Please find more information here:
array
I have been trying to execute this code, and I am getting an error:
main.cpp:11:19: error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)’
v.push_back(s[2]);
#include <iostream>
#include<vector>
using namespace std;
int main()
{
string s;
s="abc";
vector<string>v;
v.push_back(s[2]);
cout<<v[0];
return 0;
}
Indexing into a string like this:
s[2]
gives you a char, and there's no overload of push_back for vector<string> that takes a char.
Instead, you can use the initializer list constructor for string like this, to pass a string containing a single character:
v.push_back( { s[2] } );
s[2] is a character (type char), so it cannot be inserted to vectors that accept string.
The constructor of std::string
basic_string( size_type count,
CharT ch,
const Allocator& alloc = Allocator() );
is useful to create strings from one character.
Try this:
#include <iostream>
#include<vector>
using namespace std;
int main()
{
string s;
s="abc";
vector<string>v;
v.push_back(std::string(1, s[2]));
cout<<v[0];
return 0;
}
You are trying to push a single char into a std::vector of std::string elements. std::string does not have a constructor that takes only a single char as input (but it does have an operator= that does).
So, you will have to either:
Change the std::vector to hold char elements instead of std::string elements:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s = "abc";
vector<char> v;
v.push_back(s[2]);
cout << v[0];
return 0;
}
Change how you are constructing the std::string objects that you are pushing into the std::vector:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s = "abc";
vector<string> v;
v.push_back(string(1, s[2]));
//
// or:
// v.push_back(string(&s[2], 1));
//
// or:
// string tmp;
// tmp = s[2];
// v.push_back(tmp);
cout << v[0];
return 0;
}
i want the equivalent of this C# piece of code in C++
String name;
name=Console.ReadLine();
i tried the following code, but its not working!
struct node{string player_name};
p=new struct node;
getline(cin,p->player_name);
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
getline(cin,s);
cout << s;
}
Try it here at http://ideone.com/AgLUGv
The code you posted doesn't compile. It is missing a ; after player_name, for example. Here is a version that does compile:
#include <iostream>
#include <string>
struct node{
std::string player_name;
};
int main()
{
node * p= new node();
std::getline(std::cin, p->player_name);
delete p;
return 0;
}
Of course there is a simpler way of doing this, you do not need to use new/delete you can create the object on the stack. The contents of player_name are created in the heap:
#include <iostream>
#include <string>
struct node {
std::string player_name;
};
int main()
{
node p;
std::getline( std::cin, p.player_name);
return 0;
}
If you want the equivalent of your C# code, then we can remove the node struct:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::getline( std::cin, name);
return 0;
}
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();
Hello guys,
I am trying to overload the left shift bit operator, <<, to do something like:
char value[] = "Hello";
value << 2;
when doing this I would like to have it printed like: "val", so to delete the last two character; My problem is I can't manage to declare my overloading function properly.
My code is:
//the .h file
#pragma once
#include <iostream>
class Operators
{
public:
char *word;
int number;
Operators(void);
Operators(char str[], int num);
~Operators(void);
void Print(void);
friend char & operator<<(char &stream, int &nr);
};
#include "StdAfx.h"
#include "Operators.h"
#include <iostream>
Operators::Operators(void)
{
word = "";
number = 0;
}
Operators::Operators(char *str, int num)
{
word = str;
number = num;
}
Operators::~Operators(void)
{
}
void Operators::Print(void)
{
printf("\nThe String: %s", word);
}
friend char & operator<<(char &stream, int &nr)
{
return stream;
}
// Operator_Overloading.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Operators.h"
#include <conio.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char value[] = "Hello";
Operators op(value, 2);
op.Print();
_getch();
return 0;
}
You cannot overload any of the operators if they don't involve, at least, one user defined type. Your use case involves a char[N] and an int, i.e., you can't overload any operators for these arguments.