C++ :: Passing Ifstream Object Between Functions

Mar 29, 2013

Code:
void lexer(ifstream& inputfile) {
string line;
getline(inputfile,line);

[Code] ......

I am trying to pass input file between two functions. The code compiles but immediately upon running the program, there is a "bad cast" run time error.

View 9 Replies


ADVERTISEMENT

C++ :: Ifstream Object Passing To Functions

Feb 29, 2012

I am having an issue with passing an ifstream object to functions. Here is the code:

Code:
#include <fstream>
using namespace std;
void otherfunction (ifstream *ifs) {
...does stuff, like ifs->open(), then reads from the file...
}

int main () {
ifstream ifs();
otherfunction(&ifs);
}

Here is the error message:

Code: error: cannot convert ‘std::ifstream (*)()’ to ‘std::ifstream*’ for argument ‘1’ to ‘void otherfunction(std::ifstream*)’

Why can't I do that? What does "ifstream (*)()" even mean? And I don't want to change the structure of the program. I have reasons for declaring the ifstream object in the main function (because there are actually two functions that need access to the ifstream object -- neither of which is working).

Also, if I change the main function to be this instead:

Code:
int main () {
ifstream ifs();
ifstream *ifsptr = &ifs; //EDIT 2: forgot the ampersand
otherfunction(ifsptr);
}

I get the same error as above. However, if I change the main function to this:

Code:
int main () {
ifstream *ifsptr = new ifstream();
otherfunction(ifsptr);
}

I get all kinds of crazy errors about "undefined symbols for architecture _____". Here is the actual error message from my program (parseArgs is the real name of otherfunction)

Code:
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in cchumGBV.o
"std::ios_base::Init::~Init()", referenced from:

[Code] .....

View 12 Replies View Related

C++ :: Ifstream Object Won't Connect With A File

Oct 9, 2014

Is there a way to make this program work without entering the full path to a file?

Code:
ifstream ex("podatki.txt", ios_base::in); // if I type full path than program works
if(ex.is_open()) cout << "The file is open and con. with the object." << '
';
else cout << "you messed up" << '
';
int counter = 0;

[Code] ....

View 1 Replies View Related

C++ :: Unable To Pass Reference Of Object Ifstream To Constructor

Jan 30, 2013

I'm trying to pass an reference of the object ifstream to my constructor as such:

// myClass.cpp
int main(){
ifstream file;
myClass classobj = myClass(file);}

I am defining the constructor as follows, in myClass.cpp:

myClass::myClass(ifstream &file){
// do stuff
}

I get the error of "No overloaded function of myClass::myClass matches the specified type."

Also, when I build the program, it tells me "syntax error: identifier 'ifstream'"

Also, I made sure I included this:
#include <iostream>
#include <fstream>
#include <sstream>

What am I doing wrong? How can I pass an ifstream reference to my constructor?

View 3 Replies View Related

C :: Passing Into Functions

Feb 18, 2013

forget everything from before. It came out of confusion regard the supplier functions. dis_s() and read_s() the part functions work and are not any different really.

Code:

#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define MPS 10 //Max Part Size
#define MSS 10 //Max Supplier Size
}

[code]....

when I run the dis_s() function it just prints out garbage until it segments. starts with a bunch of 0 and newlines until it starts printing locations on my computer...... it worked on campus with debian and I only copied it from gmail to my computer so I really don't understand whats up(im on mint).

View 7 Replies View Related

C++ :: Getting A Value From Object Passing It To Array

Apr 17, 2014

I am writing a program for a poker game.. I created a class to get cards and create deck.. the problem I am having now is how to deal 5 cards to an array to ve evaluated later. I want to call the array player1[5]. I tried to use pointer but I get the following error

#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const char* FaceString[13] = {"Ace", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};

[code]....

View 4 Replies View Related

C++ :: Passing Object By To Queue

Dec 4, 2013

I've been working on a little project and hit a snag. I'm using nodes for a queue and stack class that were created using an existing list node class. I create an object for a student class and I want to enqueue that object.

int main() {
Queue sLine;
Customer stu;
Queue<Student &>
cLine.enqueue(cust);
}

That's basically the coding of it in main. However when I follow the error which says uninitialized reference member ListNode<Student& info>::data;

#ifndef LISTND_H
#define LISTND_H
template< class T > class List;
template< class NODETYPE >
class ListNode {
friend class List< NODETYPE >;

[Code] .....

What I may have been doing wrong? Trying to work within certain contexts.

View 2 Replies View Related

C++ :: Passing Object Into A Function

Feb 13, 2013

This is supposed to check for valid input

if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0)) {
c.integer = 0;
c.numerator = 0;
c.denominator = 0;
}
if((c.integer>0)&&(c.numerator<0))

[Code] ....
And it does just fine, until it's passed into a function, then it doesn't work:

void validInput(Mixed a) {
if(((a.integer < 0)&&(a.numerator < 0))||(a.denominator<=0)) {
a.integer = 0;
a.numerator = 0;
a.denominator = 0;

[Code] ....

It works, when the object is passed, except for two cases (one where the minus sign shifts) and whenever there is a zero or a negative integer in the denominator.

Also, I'm passing the function like validInput(c);

View 4 Replies View Related

C :: Passing Arguments To Functions

Feb 4, 2013

Having a little trouble passing arguments to functions.

I wrote this simple program to get the hang of it but I'm quite stuck. I'm sure you will be able to get at what i want the program to do...

Code:
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>

void menu(int HP,int Gold,int Armour);

[Code] ....

View 3 Replies View Related

C :: Passing Pointers To Functions

Feb 28, 2015

I have been struggling with pointers. I am trying to write a program that first asks a user to input a filename. It then checks if the file exists and if it does it passes a pointer to the next function. The next function then asks the user for a specific word to look for and the function will search a text file for the word and do some other operations. My problem is that I do not understand how to use the pointer returned by my first function as an input to another function.

The following code has the first function file_check() and the second function word_search() which I think the way I am declaring it is the problem.

Code:
FILE *file_check();
void word_search(FILE *);
int
main(void) {
FILE* check= file_check();
// word_search(check);

[Code] ......

View 6 Replies View Related

C :: Passing Variables Among Functions

Mar 6, 2015

I have this code where I am trying to retrieve the contents of the variable dev1 and dev2. for some reason when i compile and run I am getting 0 and 0.

Code:

typedef struct {
uint32_t x;
uint32_t y;
} sample;
void get_sample(sample *one)

[Code].....

View 9 Replies View Related

C :: Passing Arrays Into Functions

Nov 4, 2013

i have a program that works, but now I am trying to get function1() to work. What it has to do is bring in the array and populate it with random letters. I don't know much about functions.

Code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define maxrow 20 //defines maxrow as a constant of 20
#define maxcol 30 //defines maxcol as a constant of 30
}

[code]....

View 2 Replies View Related

C++ :: Passing Structs And Functions All In Same

Mar 8, 2013

I think i am getting confused with passing structs and functions all in the same...When I run through the program (it compiles), the functions that add coins do not add, but rather just replace an old value with a new one.

#include <cstdlib>
#include <iostream>
using namespace std;
struct coinbox {

[code].....

View 1 Replies View Related

C++ :: Passing Values To Functions?

Jul 29, 2013

I am trying to get some confirmation about how to pass to functions. If you want to assign default values to certain parameters, and have others defined inside the body of int main(), then the parameters which will have default values go at the end of the list. Is that correct?

i.e. The following code is wrong, because we cannot leave a black in the function call on the third line of the main function. However, if we switch the prototype to void Passing (int a, int c, int b = 1); and the function definition to void Passing (int a , int c, int b) everything will be okay and we can call the function as Passing (a, c).

In brief, we cannot do this EVER:
Passing( a, , c)right?
#include <iostream>
using namespace std;

[Code]......

View 4 Replies View Related

C++ :: Passing Types To Functions

Jan 12, 2013

I've got a game engine with a line-trace collision method which returns the first object it hits. I'd like to be able to pass it a class-type so that it can ignore objects of other types.

consider this pseudo-code:

Entity* TraceEntity( const Vec3f& LineStart, const Vec3f& LineEnd, const Type atype ) {
// check collision on entities, ignore entities of type 'atype'.
// return whatever it finds
}

I'd like to do this without template classes because it will result in a significant bloat in executable size every time I decided to trace for a new entity type (I've really developed a distaste for templates for this reason)

using type_info only checks for an object's deepest subclass, so it won't work for class C : public B : public A if I'm looking for classes of type B.

View 5 Replies View Related

C++ :: Passing Variables Between Functions

Jan 29, 2013

I need passing some variables between functions. Here is the first part of the code which does work.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct CarType {
string maker;

[Code] ....

Now the book says to take the following program and add a member function to the CarType class which prints the values of all of its data members. Add two more data members which are relevant for cars. Add the use of these data members to the program (to the assignment statements for MyCar, to the operator prompt and input inside the getYourCar function, and to the print function you have created).

Here is my code. Whenever I run it, it takes my assigned variables in MyCar and prints those instead of the one which the user is inputting.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
using namespace std;
struct CarType {
string maker;

[Code] .....

View 3 Replies View Related

C/C++ :: Passing A Stream Object By Reference?

May 5, 2014

I need to create a function in my program to open an input file and another function to open an output file and I need to use the files in other functions so Im trying to pass the stream object by reference but then i need a condition that tells the compiler not to reopen the file because then it will delete everything and make me input the file names again. Heres the two functions.

void InputFileOpen(ifstream &inFile) {
string file_name;
if(!inFile.is_open()){
cout<< "Enter the input file name: ";
cin>> file_name;
inFile.open(file_name, ios::in);

[code]....

View 4 Replies View Related

C++ :: Error Passing Object Reference

Nov 23, 2013

Code:

#include <Data.h>
int main(int arg_count, char** argv) {
if(arg_count!=3){
cerr<<"Files misisng
";
exit(1);

[code]...

This actually should work, because it is passing address of polymorphisms object.I have tried changing prototype of test in Data.h, but failed.passing object address/pointers in C++.

View 5 Replies View Related

C++ :: Passing Object By Pointer To Function?

May 20, 2014

I have a Qt classes as follow:

Code:

class Vehicle {
public:
void AddData(QString str, Data* data) {
_myDataMap.insert(str,data);
} virtual void Init();

[code].....

My questions are:

After the main function called d1->Modify; the data stored in _myDataMap will get modified too.

What is the more appropriate way of passing the Data through AddData in such case?

If i do AddData(const Data & data), i will not be able to use inheritance of Data, i.e passing a subclass of Data to AddData.

View 3 Replies View Related

C++ :: Passing Fstream Object To Function

Jul 6, 2013

I want my function to take 'fstream' object as an input, so the program looks like this:

Code:
#include <fstream>
using namespace std;
void test(fstream a){
a.open("test2.txt");
a << "123" << endl

[Code] ....

But I get error: 'std::ios_base::ios_base(const std::ios_base&)' is private|

View 3 Replies View Related

C++ :: Grade Average Passing In Functions?

Sep 3, 2013

My average is failing but I played and played with it and I still keep crashing.

Code: #include <iostream>
using namespace std;
void getScore(int score[], int NumGrades)
{
cout<<"How many grades do you need to enter?"<<endl;
cin >> NumGrades;
cout<<"Enter the Students Grade(s)"<<endl;
for (int i=0; i<NumGrades; i++) {
cin >> score[i];

[code].....

View 9 Replies View Related

C++ :: Passing Ints To Void Functions

Mar 19, 2014

At first i had my int variables in global scope however i cant do the so im trying to pass my variables from my main to the void functions but cant.....

View 1 Replies View Related

C++ :: Passing Structure Arrays To Functions?

Oct 31, 2013

Description: Use functions and structures to simulate storage in a warehouse

*/
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
struct Bin {std::string name; int Quantity;}; //create a structure for "Bin"

[code].....

I keep getting a linker error on every function. what am I doing wrong?

View 2 Replies View Related

C/C++ :: Passing Structure Addresses To Functions

Mar 20, 2014

What this is, is a more recent assignment and my question is if my errors are directly related to passing structure addresses to functions. I've tried several syntax variations at the beginning of my loops such as this one:

while (choice != "Q" || "q")

But the loops will not run since I introduce polar to rectangular and the choice element. My last working code was rectangular to polar and all of it worked fine.

#include<iostream>
#include<cmath>
using namespace std;
//structure declarations
struct polar
{
double distance; //distance from origin
double angle; //direction from origin

[Code] ....

View 14 Replies View Related

C++ :: Passing Object To Inherited Class By Constructor

May 1, 2013

I am trying to pass an object to an inherited clas by a constructor. I am having Cboard my board and i need to pass it to the object Cpawn.

Here under is my code:

main
Code:
#include<iostream>#include "Cboard.h"
#include "Cpawn.h"
#include "Cpiece.h"

void main(){
char location[50];

[Code] ....

View 3 Replies View Related

C++ :: Passing Object With Templated Static Array Into Another

Feb 3, 2014

This problem just seems really strange to me because it is simple yet for some reason my class cannot pass into another class. The class PASS_OBJECT has a static array (even with 1 element this doesn't work) and when I try to pass this class (after it is initialized) I seem to lose the data inside the PASS_OBJECT. Not only that but even when I declared the class OBJECT with the type of PASS_OBJECT<int> I seem to lose the integer 99. Here's the code, note that if you comment out line 89, 92 and 93 you will notice that line 90 outputs In main 2: 99 just fine but it doesn't otherwise???

#include <iostream>
const int size = 1;
template <class T>
class PASS_OBJECT;
template <class S>
class OBJECT {

[Code] ....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved