C++ :: How To Properly Overload From A Stream SOURCE Perspective

May 11, 2014

I'm working on a class that needs to read from cin, but also needs to read the input twice. I've decided that the mechanism I want to use is to write a simple wrapper class that will read from cin, then write to a tempporary file in the operator>> method, then it will, when the application calls seekg(0) to reset the file to the beginning of the file, my helper class will just switch the underlying stream from cin to the temporary file, then return reads from there.

I know there are some underlying issues to address if I need MORE functionality later, but for now, I know the basic functionality I need and from a logic level and how to make that happen. What I don't know how to do is handle the syntax of overloading the operator>> method.

I'm looking for a good syntax reference, or some sample code of how to properly overload this from a stream SOURCE perspective. I've written plenty of classes that overload operator>> from the DESTINATION perspective, but the exact same prototype doesn't work, at least not for primitives.

View 3 Replies


ADVERTISEMENT

C++ :: How To Properly Cite A Source In Code

May 24, 2013

For example, if one researched how to deep copy a map container and paraphrased a solution from a website, what is the proper format? Are there standards for citing works in code?

I am currently only citing the link to the source in my personal code and explaining what algorithm the source contains.

View 4 Replies View Related

C++ :: No Constructor Could Take Source Type Or Constructor Overload Resolution Was Ambiguous

Mar 1, 2014

i am writing this bank accounts program using structures. i haven't implemented the function before that i want to check if the data is being read and printed. When i build and run the program in visual studio it gives me the following error. "No constructor could take the source type, or constructor overload resolution was ambiguous". Now whats wrong in this program?

/* Bank Accounts Program */
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>//needed to use system() function
using namespace std;
const int MAX_NUM = 50;
struct Name{

[code]....

View 1 Replies View Related

Visual C++ :: File Stream Change Its Address During Writing Text Contents To The Stream

Apr 29, 2013

I wrote a program to write text contents to file stream through fputs, the file stream address was changed in the middle of writing text content to the stream (11% text content have been put into the file stream), that cause the file stream pointer can be evaluated problem and raise exception on stream validation code in fputs library function, my question is what things could go wrong to make file stream pointer changed its address to something else or a NULL pointer if the file stream have not been flushed and closed.

View 5 Replies View Related

C++ :: How To Overload Operator Twice

Jul 10, 2014

Here's my question. I'm coding a basic Linked List class (for the purpose of understanding and having fun, I know about STL), LList.

I have overloaded the [] operator so it returns the data of the index-th node in the list, for example, if I code

LList x;
....
cout<<x[5];

it prints the data of the 5th node in the list (for fun I decided to index from 1 to infinity).

My question: Now I want to be able to assign the value to the index-th node data, using [] and =, for example, I want to be able to write:

LList x;
.....
x[5] = 121;

How can I do that?

View 3 Replies View Related

C++ :: How To Able To Overload A Multiplication Operator

Apr 24, 2014

How would i be able to overload a multiplication operator that if, in the main example(0, 5, 0) * example (0, 5, 0) is given, it gives me 25?

View 1 Replies View Related

C++ :: Overload With Template Function

Nov 15, 2013

I am a beginner, got confused about:

template<typename T> int compare(T &a,T &b);
int compare(const char *a,const char *b);
char ch_arr1[6]="world",ch_arr2[6]="hello";
compare(ch_arr1,ch_arr2);

After running the code above,we got to know the non-template function is called. What I know is that the array arguments ch_arr1,ch_arr2 will not be converted to char * because the parameters are references in the template functions.

ch_arr1,ch_arr2 need to be converted to const char * if compare(const char *a,const char *b) were called.

I just wanna know what exactly happened behind that? and why?

View 15 Replies View Related

C++ ::  How To Overload Variables In Derived Classes

Apr 6, 2013

Is it possible to overload a variable in a derived class? Example:

struct Circle
{
int radius() const { return r; }
private:
int r;
}
struct Smiley : Circle
{
// inherits the function int radius() const, but doesn't return Smiley::r
private:
int r;
}

View 7 Replies View Related

C++ :: Why Overload Constructor Not Put Values Into Arrays

Jan 7, 2014

I'm still pretty new to classes so what am i doing in this code that is wrong.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
class BunnyInfo{

[Code]...

View 6 Replies View Related

C++ :: Possible To Overload A Class For Passing To A Function

Feb 1, 2014

I want to be able to do

someFunction(MyObject)

rather than

someFunction(MyObject.getWhatIWant())

I know I can do &MyObject, MyObject() and MyObject[0] by overloading operators but is it possible with just plain old MyObject?

this is assuming that I have no access to someFunction(), i.e, it cannot be modified.

View 7 Replies View Related

C++ :: Overload Template Function In A Class?

Jun 21, 2013

Firstly, is it legal to overload a template function in a class? This is what I did

class FILE_txt
{
public:
FILE_txt(const char* );

[Code]....

View 19 Replies View Related

C++ :: Define To Overload Virtual Function

Jul 11, 2014

I want to overload pure virtual function from 3rd party SDK class to put my debug messages like that:

errorStatus aXsaction(){printf(_T("
abort transaction"));transactionManager->abortTransaction();}

#define transactionManager->abortTransaction() aXsaction()

But compiler complains on the minus sign:
error C2008: '-' : unexpected in macro definition

Is it possible to trick the compiler?

View 4 Replies View Related

C++ :: Variadic Templates And Overload Resolution

Jan 2, 2014

I'm trying to learn how to use variadic templates, and I decided a great example would be serializing a series of types into a stringstream:

Code:
// Send a fully constructed message.
virtual void send(ostringstream &msg) = 0;
// Construct a message from the arguments and send it.
// This is the usual entry point.
template <typename ...Args>
void send(Args ...args {

[Code] ....

This works fine, so far as I can tell. However, I decided to see if I could specialize the way certain types are serialized. I tried using a Google Protocol Buffer object as an example, and added this:

Code:
// Handle a protocol buffer type while constructing a message.
template <typename ...Args>
void send(ostringstream &msg,
const google::protobuf::MessageLite &protobuf, Args ...args) {
std::string msg_str = protobuf.SerializeAsString();
msg << msg_str;
send(msg,args...);
}

I would expect this overload to be preferred over the generic T overload when a protobuf object (which always inherits from MessageLite) is passed into send() anywhere in the list. However, this is not happening. I am getting an error message to the effect that << doesn't know how to deal with my concrete type, pointing at the T overload.

View 4 Replies View Related

C++ :: Force Derived Class To Overload Certain Operators

Dec 14, 2014

Is is possible to force derived classed to overload certain operators, like we do with pure virtual functions?

Is this possible to dynamically bind objects to their respective overloaded operators?

I am getting errors with undefined references to my base class vtable when I hackly try to overload: Code: operator+ I am not sure whether this is possible.

View 7 Replies View Related

C++ :: Dealing With Operator Overload Function Failure

Aug 23, 2014

Say I have a class that requires dynamic allocation to implement a few of the operators. Take "+" for example; I need to create a new object to hold the sum of the two parameters whose size is not known at compile time.

I'm pretty sure the standard way to indicate a failure inside the overloading function would be to throw an exception. However I am currently involved in an embedded(ish) project where the spec. says no exceptions are to be used.

I think I have 2 options:

1. Return an "invalid" object (with a flag indicating an error has occurred) and check for this after each operation.

a = b + c
if (a.err)
// handle error
or

2. To forsake operator overloading entirely and think up a new way of doing things where all functions that involve dynamic allocation can return error codes. but this seems rather terrible too as I may end up with something like:

objA a
if (add(&a, b, c) == -1) // assuming b and c are initialized before this snippet starts
// handle error

Is there a number 3 that I haven't thought of? It seems that not allowing exceptions is fairly common even in the non-embedded world [URL] so how is this normally done? or is operator overloading usually avoided when exceptions are not allowed?

View 3 Replies View Related

C++ :: Overload Delete Operator With Specific Arguments

Sep 27, 2014

We're trying to overload the delete[] operator with specific arguments. Which is the right way to call it? We use the GNU compiler and obtain compiler errors with all of these samples:

#include<iostream>
using namespace std;
typedef unsigned int P;

[Code]....

View 2 Replies View Related

C++ :: Overload Virtual Member Function In Polymorphism?

Nov 30, 2013

I defined a virtual class and three other classes based on it. I want to use them like this:

int main() {
Dirichlet_t D;
Neumann_t N;
Cauchy_t C;

PDEBoundary_t * B1=& D;
PDEBoundary_t * B2=& N;
PDEBoundary_t * B3=& C;

[Code] .....

but I got two major errors
1: "object f abstract type is not allowed" error.-----why not?
2: "the derived class must implement the inherited pure virtual method"-----Did't I?

View 15 Replies View Related

C++ :: Union Of Two Vector ADT Bags - Operator Overload

Apr 30, 2014

I'm trying to come up with the union of two Vector ADT bags, so I have to overload the '+' operator, but I'm getting a bunch of error messages saying:

VectorBag.cpp: In instantiation of ‘VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType>) [with ItemType = int]’:
proj2.cpp:161:42: required from here
VectorBag.cpp:81:24: error: no match for ‘operator[]’ (operand types are ‘VectorBag<int>’ and ‘int’)
newBag.add(anotherBag[i]);
^
Here is the function to overload the operator:

template<class ItemType>
VectorBag<ItemType>
VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag) {
VectorBag<ItemType> newBag;
for (int i = 0; i < anotherBag.getCurrentSize(); i++)
newBag.add(anotherBag[i]);
}

The add() function is pre-defined by me somewhere else in the code. It basically does push_back().

View 4 Replies View Related

C/C++ :: Overload Two Different Operators One Inside Class And Other Outside Using Friend

Nov 23, 2014

I'm trying to understand the basics of oop ...

#include <iostream>
using namespace std;
template <typename T>
class max_vector {
private:
T* elemente;
int lungime;

[Code] ....

The purpose of this program is to overload two different operators one inside the class, and the other one outside using friend. The problem is that i get 1 error at the '*' one.

View 1 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++ :: Call Text File In Input Overload Operator?

Nov 12, 2013

i am doing some practice problems and i can't seem to figure out how to do this. basically we have a students number of test scores, then the name followed by the scores they have in a text file. Then we have to make a class with a constructor, copy constructor, destructor, and overload the = operator and the input and output operator. Are we suppose to call the text file in the input overload operator?

Here is what i have so far.

This is my header file.

#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>
using namespace std;
class StudentTestScores{
private:
string studentName;

[Code]...

i am 100% sure the overloading the input is wrong

here is the implementation of the constructor copy constructor and desctructor

#include <iostream>
#include "StudentTestScores.h"
using namespace std;
StudentTestScores::StudentTestScores(string name = "", int numScores = 0)
{
studentName = name;
numTestScores = numScores;
if (numScores <= 0)
testScores = NULL;
else

[Code]...

and here is the notepad file

3
Justin Bieber491.469.184.681.081.5
Miley Cyrus380.080.090.083.3
Kim K490.575.661.481.677.2

The program is suppose to use all the information and read from the notepad and output the exact things as the notepad file

View 7 Replies View Related

C++ :: Overload Operator With Stack That Implements Linked List

Nov 10, 2013

struct Node {
int entry;
Node *next;
Node(); //1
Node(int item, Node *link = NULL); //2

[Code] .....

Implement: 1 2 3 4 5
and overload operator <<, >>, =

View 1 Replies View Related

C++ :: Error When Using Volatile Object In Overload Assignment Operator

Jul 27, 2012

/*using GENERIC_COMMAND* A; as volatile generates error. but here i have to use union object as volatile i.e. volatile GENERIC_COMMAND* A; */

#include <iostream>
using namespace std;

typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
union GENERIC_COMMAND {

[Code] .....

View 14 Replies View Related

C++ :: Write Prototype Of A Member Function To Overload Insertion Operator

Apr 10, 2014

Consider the class specification below. Write the prototype (i.e. header) of a member function to overload the insertion operator (i.e. <<). The << operator is to output the data members of an instance of class StudentTestScores into an output stream. Your definition should allow for chaining of output operations (e.g. cout << x << y; where x and y are of type StduentTestScires).

#include <string>
using namespace std;
class StudentTestScores{
private:
string studentName;
float *testScores; // used to point to an array of test scores
int numTestScores; // number of test scores

[code]....

View 1 Replies View Related

Visual C++ :: Operator Overload Not Defined Error When Type Accessed Through Const Struct

Oct 17, 2012

I have a basic vector/point class where I've overloaded a bunch of arithmetical operators:

Code:
#pragma once
class point {
public:
point() {
}
point(float p_x, float p_y, float p_z) : x(p_x), y(p_y), z(p_z)

[Code] ...

I can use it fine like

Code:
point p(50,50,50);
point q(50,50,50);
point t = p * q + q;

However when I put this point type into a struct and try to access the members after passing it through by const reference:

Code:
struct sextic {
point a,b,c,d,e,f,g;
};
inline static sextic sexticDifference(const sextic &p_sextic1, c

[Code] ....

This gives me an "operator not defined" error for compilation.

View 2 Replies View Related

Visual C++ :: Circular Buffer - No Instance Of Overload Function Matches Argument List

Nov 25, 2014

I'm having some issues with my code. For the produce function i am getting an error saying 'no instance of overload function produce() matches the argument list' and also for the lines buffer[head].data = message; buffer[head].time = current_time i get an error saying 'expression must have pointer to object type.

In the code i'm not sure if i passed the variables to the function correctly. I have attached the code .....

code produce.txt

View 14 Replies View Related







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