C++ :: Streaming Input Through Two Constructors (two Classes - Using ADTs)

Jan 7, 2013

I have a main .cpp file which contains int main(int argc, char** argv) and 2 included personal headers which correspond to their linked implementation files.

What I am trying to do is use ifstream to pass integer values from a text file into my program, have the program execute, and then output those modified integers back into the same text file. I have tested the "program" portion of my project using declared values. I know that my program does what I have intended for it to do, but I can't seem to properly inject any outside data using files.

Here's what I have (not including all of the unrelated objective code):

Main.cpp

int main(int argc, char** argv) {
file File;
std::string gameFile = File.findFile(); // prompts user for existing file
if(gameFile.size() > 0) { // load saved game
std::ifstream in;
in.open(gameFile.c_str());

[Code] .....

In game.cpp, I am unsure of Player = new player; and Player = new player(data);. I have never tried doing this. This is simply my best guess of how to make this mess work properly. Hopefully I am close?

Game.h:

#ifndef _GAME_H_
#define _GAME_H_
#include "player.h"
class game {
std::string loadedGame;

[Code] .....

In game.h, I haven't yet tried anything like player* Player;. As stated in my comments above for game.cpp, I have no clue how to go about this.

player.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include "player.h"
player::player() {
srand(time(0));

[Code] .....

In player.cpp, I really have no clue where to start with the issue. I'm not even exactly sure if any values are being passed to these variables. Though, I honestly haven't taken much time to problem solve. I don't want to waste a lot of time just to find out that my attempt is incorrect and/or unconventional.

player.h

#ifndef _PLAYER_H_
#define _PLAYER_H_
class player {
public:
player(); // default

[Code] ....

So really, my concerns are: Is my attempt mostly correct? If not, why?

View 1 Replies


ADVERTISEMENT

C++ :: Constructors In Derived Classes

Apr 7, 2014

As long as no base class constructor takes any arguments, the derived class need not have any constructor, if one or more arguments are used then it is mandatory for the derived class to have a constructor and pass the arguments to base class constructors. While applying inheritance, we usually create objects using derived class. Then it makes sense for the derived class to pass arguments to the base class constructor. When both the base and derived class contain constructors ,the base class constructor is execute first.

In case of multiple inheritance, the base classes are constructed ,in the order in which they appear in the declaration of the derived class. Similarly in a multiple inheritance the constructors will be executed in order of inheritance. Since the derived class takes the responsibility to supply initial values to the base class,we supply the initial values that are required by all the classes together where the derived class object is declared.

The constructor of the derived class receives the entire list of values of arguments and pass them on to the base constructors int the order in which they are declared in the derived class

View 1 Replies View Related

C++ :: Constructors / Classes For Expression Tree Program

May 7, 2012

Previously I made a infix postfix calculator with integers only using structures, which had only three files

1.main.cpp,
2.arithmetic_expression.cpp
3.arithmetic expression.h,

I manged eventually to bring it together. now there are 5 files which are using classes, constructors and destructors and there can be letters in the expression that can have assigned values. Newnode, struct node and all that fine and dandy stuff made sense, this doesn't. What needs to be done in those empty functions in arethmetic_expression.cpp? It was going well until I reached createxpressiontree{ in th arithemetic_expression.cpp and those constructors in there. At that point I missed my structures and was totally confused by the constructors. I know constructors are supposed to create objects, but then I dont know what i am creating the object for and what that object is supposed to do.

1.main.cpp
2.arithmetic_expression.cpp
3.tree.cpp
4.arithmetic_expression.h
5.tree.h

Main.cpp

Code:

#include <iostream>
#include <string>
#include <vector>
#include "arithmetic_expression.h"
#include <map>
int main() {
// arithmetic_expression expression1;
// Testing the RPN input

[Code] .....

View 1 Replies View Related

C++ :: Streaming Bytes Into Integer

Sep 4, 2012

I have a FILE stream, and I want to create a function that streams a specified number of bytes (up to four bytes) and returns the value of this, as an integar.

For example, supposing that my FILE has the following data, in hex: 74 C8 02 33 F2 7B....... I then want to stream three bytes and store this as an integar. So, I want to stream "04 08 02". The data stored in the integar should then be "00 74 C8 02", because I have not streamed anything into the first byte. By converting the hex to dec, the integar should then be of the value 7653378 (if it is unsigned).

To try to achieve this, I have written the following function. I create an integar and initialise it to zero, then take each byte from the stream, and OR it with my integar. Then, I shift the integar left by 8, and read the next byte, and so on.

The problem is, when I convert "c" to "c_int", it adds on a load of 1's to the left of the "c" data. This then means that the OR comparison changes all those bits in my integar to 1.

How to solve this? I am also wondering whether there is a much more simple way of doing this, rather than having to write my own function....

Code:
int StreamFileToInt(FILE *fp, int num_bytes) {
char c;
int c_int;
int x = 0x0000;
for (int i = 0; i < num_bytes; i++) {

[Code] ....

View 4 Replies View Related

C++ :: Radio Station - Streaming Audio On The Web

Jan 22, 2013

work for a radio station as their webmaster, and we have been looking into online streaming options for the last few months for our website. Pretty much everyone charges a monthly fee for this type of service and I feel like it is something I am capable of doing myself.

At the station we have a computer dedicated to operating the music/talk. On this computer there is a program that ques all the music, ads, ect.. This would be my audio source: Either the program playing all the music, or the computer itself.

The problem I have been pondering all night is how would I access this input source? The only audio input I am familiar with accessing is .mp3 or .wav files. How can I code my program to access this audio the program is producing and stream it to people on the web?

View 1 Replies View Related

C++ :: WHILE Loop Error / In Streaming Data File (with Functions)

Nov 2, 2014

I have a working lab project with a loop error. Code posted in second post. Here's the requirements:

*
- program has to be able to handle file failure
- must loop (prefer to use a while loop here), read in the data, calculates values, etc. and then output the results.

*
The input data file includes

1. a 1 or a 0 indicating that there is a set of employee data following

2. the hourly rate of the employee

3. the humber of hours worked

4. the number of dependents

5. a 1 or a 0 indicating whether the employee is full time (1=full time).

Problem: At first I wouldn't loop back... so I moved the curly brace above the return0;

View 2 Replies View Related

C++ :: Tag Dispatching Constructors

Dec 24, 2014

#include <iostream>
#include <string>

struct A {
struct Tag{};
std::string name;
int value;
A (const std::string& n, int v) : name(n), value(v) {}
A (const std::string& n, int v, Tag) : name(n), value(v) {std::cout << "Constructor with Tag called.

[Code] ....

How to avoid having to type out a second near-identical constructor for any class like D and E which have specialized constructors different from A? You can imagine the nuissance this causes if there are many classes like D and E (and with many parameters) that need the Tag parameter. The nuisance will be there to when making changes to the constructors. Delegated constructors I don't think will work because of passing `tag` into the parent's constructor. Is there some sort of inheritance trick I can apply simultaneously to all such classes to get them all to behave like B and C's constructors?

View 2 Replies View Related

C++ :: Constructors In A Singleton

Apr 6, 2013

I need to implement a singleton, so I've been reading about it online and I'm still not quite sure about all the types of constructors I need to declare:

Code:
class my_singleton {
private:
my_singleton();
my_singleton(my_singleton & X);
my_singleton(const my_singleton & X);

[Code] ....

Is this OK?

View 8 Replies View Related

C++ :: Array Of Classes With Classes Inside

Oct 5, 2013

I have an array of (Student)classes created in Manager.h, which contains a new instance of class Name (name),(in Student.h)How would I go about accessing the SetFirstName method in Name.cpp if I was in a class Manager.cpp? I have tried using Students[i].name.SetFirstName("name");

// In Manager.h
#include"Student.h"
class Manager
{

[Code]....

View 2 Replies View Related

C++ :: Multiple Default Constructors Specified

Dec 16, 2014

I have an inherited class that essentially manages a Qt Window.

It is as follows (prototype below):

class QTMyOpenGLWindow : public QWindow, protected QOpenGLFunctions {
Q_OBJECT

[Code] ....

Now, I can understand the confusion of the compiler, but the functionality as I laid it out works for me (I can create the class with just specifying the parent and also have the option of preventing auto-initialization when creating). But, is there a better approach?

View 3 Replies View Related

C++ :: Copy Constructors In Inheritance

May 16, 2013

#include <iostream>
using std::cout;
using std::endl;
class CBox // Base class definition
{
public:
// Base class constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv)

[Code] .....

This example produces the following output:

// Derived class copy constructor
CCandyBox(const CCandyBox& initCB): CBox(initCB) {
std::cout << std::endl << "CCandyBox copy constructor called";
// Get new memory
m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];
// Copy string
strcpy_s(m_Contents, strlen(initCB.m_Contents) + 1, initCB.m_Contents);
}

It will work right? Cause when I do "CBox(initCB)" it only sends the BASE part of the derived object to the base class copy constructor, is it copy or default?

View 6 Replies View Related

C++ :: Using Constructors In Template Class?

Sep 13, 2014

The code below references to a header file and implementation .cpp file, which are not important. My question is what is the proper way to use a constructor in a main file. I have been getting "invalid use of" errors when using letters.Pair(a,b), where Pair(T a, T b) is a constructor that accepts arbitrary type T of variables 'a' and 'b'. So I played around a bit and suddenly found a syntax that works. I need verification for the syntax below:

#include <iostream>
#include "pair.h"
#include "pair.cpp"

[Code].....

Are the comments with the asterisks correct? As in this is always the way you initialize and assign? So letters.Pair(a, b) is not the right way to use constructors?

View 2 Replies View Related

C/C++ :: Calling Constructors With Parameters

May 28, 2014

I created 3 Rectangle pointers. And later in the program, I would like to modify these existing Rectangles by calling constructors with parameters. Is this possible? I have a sense that it involves creating overload operators, but I am not sure how to do it, or if that's the correct path.

#include <iostream>
#include <string>
using namespace std;
// Base class
class Shape {
protected:
int width;
int height;

[code]....

View 6 Replies View Related

C++ :: Link Error Although All Constructors Appear To Be There

Feb 16, 2015

I'm getting a massive 1300 char link error with VC10. It appears to complain that it can't see the constructor although the constructor is definitely there.

Error:
test_GatewayNS.obj : error LNK2019: unresolved external symbol "public: void __thiscall std::allocator<class BinarySearchVector::ElementTemplate<class Gateway,unsigned __int64> *>::construct(class BinarySearchVector::ElementTemplate<class Gateway,unsigned __int64> * *,class BinarySearchVector::ElementTemplate<class Gateway,unsigned __int64> * const &)"

[Code] ....

However, the constructors seem to be there and if I copy them into my program just to make sure - the compiler complains that they are already defined:

namespace BinarySearchVector {
template <class ElementType, class IdType> class ElementTemplate //allows comparison functions to be redefined {
public:
ElementTemplate(IdType myId) : id(myId), tickCount(0), requestingDeletion(false) {};

[Code] ....

Any clues as to what I'm missing ?

View 2 Replies View Related

C++ :: Copy Constructors For A Dynamic Array

Dec 13, 2013

I am trying to figure out copy constructors for a dynamic array and I am definitely missing something. If I go into the copy constructor routine during debug, the values appear to be correct but they don't percolate up to the newly created object. I'll post a portion of the code below:

Code:

// include header files for the classes that are being used
#include "stdafx.h" //

NOTE: THis reference must be added to all cpp files in Visual Studio Express 2013

#include <iostream>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;
const int ARRAY_SIZE_DEFAULT = 32;
class vectorOfInt {
public:

[code]....

The size of c is 0. Values of a were not copied to c, although they appear to do so within the copy constructor routine.

View 7 Replies View Related

C++ :: Default Constructors And Header Files?

Jun 8, 2013

I'm working on trying to figure out constructors and header files. Can ya'll help me out with this? I'm sure my code looks like a mess as I tried to piece together different solutions I've found. There's also an attempted copy constructor and operator function. Basically my problem is my source file says there is no default constructor for my class type. Here's my header code:

#include <iostream>
#ifndef _car
#define _car

[Code].....

View 8 Replies View Related

C++ :: Writing Test Programs Involving Constructors

Jan 30, 2014

I am currently trying to write a test program involving constructors. I know what I am about to ask is pretty basic stuff but Visual Studio is not recognizing when I declare strings for some reason. My code isn't completed yet but as I am typing string in to declare the variable in the class Visual Studio is not recognizing string as a usable value.

Code below:

#include <cassert>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;
class college {

[Code] .....

Like I said... this is completely unfinished I just need to understand why my strings aren't being recognized.

View 2 Replies View Related

C++ :: Class Constructors And Data Member Initialization

Oct 29, 2014

I recently discovered the new - new to me anyway! - feature of modern C++ that allows you to set the initial value of a data member when you declare it:

class CINTWrapper{
private:
int m_iData=0;
};

This even extends to calling member functions that work with initialization I believe:

class CStringWrapper{
private:
wchar_t* Allocate_Array(const int iBufferSize);
wchar_t* m_pString=Allocate_Array(1);
};

At first, this seemed an extremely useful piece of functionality that C++ had been lacking all along. However, the more I thought about it the more it struck me this feature actually undermines one of the principle design elements of the language - that being the Constructor.

As I understand it the primary purpose of the Constructor is specifically to give the programmer a place where it is guaranteed he can always initialize his data members before anything else is done with the class. However, given the new initialization rules this is no longer necessary. So it largely seems to me that Constructors as a whole are no longer necessary either! Copy-Constructors are a special and vital case. Admittedly when I was using them for their intended purpose I hated either the redundancy you had to introduce across multiple Constructors; those with and without arguments and so on, or alternately the fine tuning of helper-functions to do common initialization between these variants. Now however I sort of regret this cast-iron rule has been taken away.

As a last point, I am trying to change the way I think about programming. I am trying to employ more objects than pure C-style ('int' or 'double', etc) data types and especially to move into templates (although absolutely NOT the Hewlett Packard template library!). Given my current understanding of inheritance in particular it seems to me that using pre-initialized data members rather than Constructor-initialization makes object derivation even more complicated, not less so.

View 16 Replies View Related

C++ :: Multiple Constructors Calling Parent Constructor

Dec 6, 2013

I have a class that extends another class, and I want multiple constructors in the child class, but the child constructor needs to call the parent constructor. This is what I have

In the child class:

ChildClass::ChildClass() {
ChildClass(1);
}
ChildClass::ChildClass(int i)
: ParentClass(i) {
// do stuff
}

In the parent class:

ParentClass::ParentClass(int i) {
// do stuff
}

In my main program:

ChildClass child1;
// do stuff with child1
// breaks
ChildClass child2(1);
// do stuff with child2
// works fine

Using the default constructor breaks my program at runtime, but using the one with a parameter works fine. The default constructor calls the other with the same thing as the main part in the program, so I would think this should make no difference, but obviously that isn't the case.

View 3 Replies View Related

C/C++ :: Overload Operator With Friend Function Using Constructors

Dec 26, 2014

I want to overload prefix and postfix increment(++) operators with friend function. I also have to use the constructors for this. How can I do this? in C++

View 4 Replies View Related

C++ :: Calling Base Class Constructors From Derived Class

Mar 30, 2013

I'm having some difficulties in understanding the topic which I stated above.

View 5 Replies View Related

C++ :: How Do Two Classes Interact With Each Other

Aug 26, 2013

I want to know that how objects of two different classes interact with each other???

View 1 Replies View Related

C++ :: How To Know If Classes In Program Are Being Used

Aug 13, 2014

In this program the intention is to create a menu driven program for a pizza restaurant. I have to use a class called Pizza and have to include at least three public functions; one is called SetSize, another one is called Display, and the last one is called ComputePrice. A small pizza is worth $10, a medium is $14, and a large is $17. Each topping is worth 2 dollars. I know that the program runs correctly, but I have doubts over the classes and function actually being utilized correctly or at all.

Program:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Pizza {
private:

int size;
int style;

[Code] .....

View 8 Replies View Related

C++ :: Using Multiset With Classes?

Oct 23, 2013

I'm trying to use multiset with a user defined class "edge". I'm trying to use the multiset as a priority queue, and I've created a "less<edge>" via operator<() overloading.

For some reason, I cannot insert edges into the multiset.

I understand that I might also have to create an "allocator". I got some ideas for creating it at [URL], but still don't know how to define size_type and difference_type.

Attached is my skeleton code, running on Windows 7 (32-bit), under Netbeans IDE, using Cygwin g++ 4.7.3.

How can I get this to work? What is important is that I get a priority queue working with my edges, prioritized by the weight.

#include <iostream>
#include <set> // for multiset
using namespace std; // assume std libraries (i.e. std::XXX)
class edge { // node, weight pair
public:

[Code].....

View 3 Replies View Related

C++ :: Derived Classes From DLL

May 5, 2013

I've created a base DLL for all my future DLL's, a way of getting version numbers and such and that compiles fine, but I can't add it into a class for a new DLL. All the headers do have an appropriate cpp to define the function declarations (and they compile fine).

All for the base DLL I have:

LibVer.h
Version.cpp
Function.cpp

LibVer.h

#pragma once
#include <vector>
#define DLLEXPORT 1
#define DLLIMPORT 2
#define DLL DLLIMPORT

[Code] .....

View 6 Replies View Related

C# :: Can Use Variables From Other Classes

Feb 23, 2014

im creating an address book. One address book contains a ListBox, New User button, Edit User and Remove User button. The first form is suppose to allow you to view the users you've created on the ListBox and you can decide whether you want to remove it, create a new one or simply edit the user. Now The second form simply contains labels and textbox along with a save button. I'm having a bit of issue figuring out the ListBox. I want to be able to create a user and have the user be posted on the ListBox. I read that i must instantiate listbox then simply add it. Now on my form2 i have a for loop that loops through an Array of String were all the users will be created on. How can i call that array of string on to the form1?

Form 1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

[code].....

EDIT:I just figured out that to call a variable from one form to another you simply instantiate the form then simply call it. PS. must be set to public:

ListBox1 createUser = new ListBox1();
createUser.userString[0];

why doesnt it show the windows when i run without debugging?

View 1 Replies View Related







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