I want to design a class that will open a text file and manipulate the data in it. And I have to use all these functions from fstream like ifstream, ofstream, seekg etc. The problem is that I can't get the first part to work (getting my constructor to open the file using ifstream). I've posted my test.h file below.
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::ifstream;
class Test {
I need to a simple example for writing class' objects to a text file using fstream , I tried to search the forums and I found binary examples but not for to a text file.
So I have a small program which is supposed to write and read multiple objects from a file, but for some reason it doesn't write the information when I use "fstream" to create the object from the fstream class, but it does when I use "ofstream". Also, it doesn't read the information from the file no matter if I use "fstream" or "ifstream". I watched a video where this exact code worked just fine, but it just won't work when I run it. I'm using Dev C++ 4.9.9.2, I don't know if that has anything to do with it, I also tried it with Code::Blocks, didn't work either.
Here's the code.
#include <iostream> #include <fstream> using namespace std; class Person
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h" #include "unorderedLinkedList.h" template<class Type> class Stack: public stackADT<Type>{ template <class T> struct nodeType { T info; nodeType<T> *link;
But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:
error: argument of type 'void (Basic::)()' does not match 'void (*)()'
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight() fight(Player p1, Player p2) { p1.func2(); } //using function fight() fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
Just a few moments ago i was just doing foolish things in c++ and discovered something new. Though some of you might have known this, for those who dont know, take a look at the follwing 2 small programs,
so here is my problem. i think u wud have figured out what m trying to do above. am actually calling the main() of the class and from there, i want to call the usual main... the problem is, during A.main()'s run, if i refer to main(); , that call represents itself, that it, it is like it calls itself. How on earth can i call the outside main?
If you are doing some big program, usually, how do you organize the files? Put the class and its member in head file, but where to declare non member functions and where to define them? I don't want to put them all in one cpp file. If not, how to make them visible to the main cpp file?
When I tested it, the file name in getName() outputted the right file name. However the file name in loadName() does not print out anything in the console window.
I want it to be so that the strings in [I]name_addTXT.c_str() in both functions are equal.
I have a big problem with searching a solution for getting access on getters and setters of the derived classes of an interface.
Interface:
class IfParam { public: IfParam(); };
Now I have a derived class for each needed datatype. E.g.
class Int32Param: public IfParam { public: Int32Param(); Int32Param(IfParam* l_param); int getValue(); void setValue(int l_value); private:
[Code]...
My Problem now ist getting access to the getters/setters over the interface. I wish I could initialize the Params like this:
IfParam* param = new Int32Param(); param.setValue(12);
IfParam* param = new StringParam(); param.setValue("String");
But to have access to the getter/setter I have to declaire the functions in the interface as generic functions. But how? I tried to use temlates, but then i have to declaire IfParam as IfParam<int>. Thats a problem because in my original program I do not know which TypeParam the IfParam interface will be initialized with when I create the pointer.
Im currently working on a class assignment and the pseudo code containing the instructions that I need to complete list:
//-------------------------------------------------------------------------------------------------------------- // CTOR: Point() // // DESCRIPTION // Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0). // // PSEUDOCODE // Call Init() and pass 0, 0, 0, 0, and 0 as the parameters. //--------------------------------------------------------------------------------------------------------------
My code for this is:
Point::Point(){ void Init(0, 0, 0, 0, 0); }
The code I wrote for the function Init is here:
Point::Init(int mX, int mY, color mColor){ mX = 0; mY = 0; mColor = pInitColor; }
My problem here is that whenever I try calling this function in the point class, I get an error next to void Init saying incomplete type is not allowed. Also visual studio is telling me that it expects a ')' after my first zero in that line.
I could do the assignment if all i had to do was create a function within one file, call it in the main class, and that would be done with it. no, my teacher wants us to have our main program, a header and a separate class file and create functions in it in which we can then use in the main file.
This is the main file:
#include <iostream> #include "call.h" using namespace std; int main() { int length; //holds the length of the call in minutes int hour; //holds the hour od the day(0-23 military time)
I've been writing my own implementation of quicksort in a .h file. I've gotten to the point where I think I'm done with the algorithm, so now I'm trying to make it so that if someone includes "quicksort.h", they will only have access to certain functions in the file. Kind of like public and private functions in a class, but without the "class" part of it. How do you do that?
An object of this class will return a normal random when it's member function operator()() is called:
template<typename RG> double class NRG::operator()() { static int flag = 0; static double N2 = 0.0; if(flag==0)
[Code] ....
However, when I run this I get an error which says:
C:UsersavadhootDesktopb.cpp|69|error: 'template<class RG> class NRG' used without template parameters| C:UsersavadhootDesktopb.cpp|69|error: expected identifier before 'operator'| C:UsersavadhootDesktopb.cpp|69|error: two or more data types in declaration of 'operator()'| ||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
In the thread "Making a argument optional to function", it is stated that to set default values for arguments of a function you can simply do so in the function definition, like:
int myfunc(int a, int b, int c=3) {...}
This then automatically puts c to 3 in the function body if a call like myfunc(1,2); is made, if I understood correctly. However, this does not seem to hold for class functions. For example, something like:
class classy { public: int class_func(int, int, int); // class function prototype } int classy::class_func(int a, int b, int c=3) {...}
fails to compile. What I would like is to be able to call class_func outside of this class (by including it as a header in another macro), optionally specifying c. If c is not specified in the call, it should use a default value.
I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
Here's my code:
Class #define NUMNODES 10 using namespace std; #pragma once class Prog3Graph
#include "stdafx.h" #include <iostream> #include <math.h> using namespace std; class Calc {
[Code] ....
when i built it, it showed the following errors:
1>------ Build started: Project: rough, Configuration: Debug Win32 ------ 1> rough.cpp 1>e:c programs ough ough ough.cpp(17): error C3872: '0xa0': this character is not allowed in an identifier 1>e:c programs
Is there any way to track what functions from what class are called at runtime? What I mean is a list of functions or classes which have been called at runtime.
I have a program that uses class functions to enter and print out info. The problem is with the second function answers(). Here is the whole cpp file. In the answer function I need to use an exception to exit when its the end of an array. I could just be doing it wrong. I used try/catch originally but when I used it, it caught the exception but ended the whole program.
Here is the assignment: (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (How the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!
class Album { private: char * artist; // band or singer’s name char * title; // title of the album
[code]....
The input will be an array. My questions: First, am I on the right track?
When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?
also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?
what is the difference between *& and * as parameters?