C++ :: Overloading Char And String Operators?

Dec 20, 2013

I build a nice class that i need the output be possible in char * and in string. But how I can do for the compiler don't be confused between them?

Code:
operator char*() {
return (char *) result.c_str();
}

These works fine. and for string, i must casting it. but i want do anotherone for string... how can i do it? I tried:

Code:
operator string() {
return result;
}

But the compiler stays confused or something

View 14 Replies


ADVERTISEMENT

C++ :: Using Bitmask Operators - String Bin To Char

Mar 3, 2013

So this is the code I have so far which puts a bitmask seperator base on what I choose. My problem is when Im trying to do them in this manner.

1. 0 to 31, with minimum width set to 8, and separating between every 4 digits.
2. 2^0 to 2^16, with minimum width set to 17, separating between every 8 digits.
3. (2^0-1) to (2^16-1) with minimum width set to 16, and no seperation between digits.

I try doing number 2 called "String multyplyByTwo" but seems to be getting errors.

#include<iostream>
#include<string>
using namespace std;
string binToChar( const int n, unsigned minWidth = 8, unsigned sepMask = 0x11111110 ) {

[Code] ....

View 3 Replies View Related

C++ :: Overloading Subscripting Operators?

Nov 7, 2014

I have two class GameOfLife and Cell and i want to overload square braket for class GameOfLife."if g is a GameOfLife object, g[10][5] will return the Cell at row 10 and column 5. If there is no such Cells, then it will return a new Cell with position (-1000,- 1000)."

but if g[10][1000] and 1000>cols,then it returns different Cell exp (3,2) How i do control the col ( [row][col] )?

Code: vector<Cell> GameOfLife::operator [](const int row){
vector<Cell> rowCell;
for(int i=0; i<cols; ++i)
{
if( isLive(row,i) )
rowCell.push_back( Cell(row,i) );
else
rowCell.push_back( Cell(-1000,-1000) );
}
return rowCell;
}

View 6 Replies View Related

C++ :: Overloading Comparison Operators For Using In A Set

Oct 30, 2014

I have a small piece of code that used the set::insert function on a set of myClass. For that, I need to overload the < and > operators, which I have, but I still get a huge error saying it can't compare.

set<MyClass> mySet;
MyClass myClass

All the class information gets filled in. Then, I try to insert...
mySet.insert(myClass);

bool operator<(MyClass &lhs, MyClass &rhs) {
return lhs.name < rhs.name; //name is a string
}

The error says
...stl_function.h:230:22: error: no match for 'operator<' in '__x < __y'
MyFile.h:80:6: note: candidate is bool operator<(MyClass&, MyClass&)

View 5 Replies View Related

C/C++ :: Overloading Comparison Operators

Aug 28, 2014

I made a program that allows the user to enter information of credit cards on an array of size 5, however I need to allow the user to compare the five credit cards with each other and I am having problems with this particular part. I made my bool operator functions for the operator< and the operator> but how to make the user be able to select which cards he wants to compare and how to compare them. My code is the following:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 5;
enum OPCIONES {CARGAR=1, ABONAR, NADA};

[Code] ......

View 2 Replies View Related

C++ :: Does Polymorphism Affects Operators Overloading?

Nov 24, 2013

I must overload [] but at the same time I must use polymorphism. I wonder if using polymorphism affects operators overloading since when I modified the first class by writing "virtual":

virtual void mostrarDatos(char*, char*, char*);
virtual void calcularEdad(int);

So I can do the polymorphism, it affects the part of the code where suppose to do an addition:

s=student1+=student2;
t=student3+=student4;
u=s+=t;

if I do that, it shows some strange numbers instead of the right ones. Here is the complete code:

.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class persona {

[Code] ......

View 2 Replies View Related

C++ :: Overloading Stream Operators On Derived Classes

Apr 16, 2014

S I want to have different >> operators for several derived classes. Has I tested...

Code: class base{
friend std::istream & operator>>(std::istream & in, base & v);
public:
base();

[Code]......

I noticed that the base operator is the only one being called for all 3 objects. which makes sense and now that I think about it I am more surprised that the "derived operators" compiled at all.

Still, how would I go about declaring different overloaded operators for the different classes?

View 4 Replies View Related

C++ :: Arithmetic Operators Overloading For Class With Pointer

Nov 11, 2014

I am stucked in a problem of overloading arithmetic operators such as "+,*" for a class in the form

class Point {
int N; // dimension of the point
double *Pos; // length of N
}

My assign operator is :
Point& Point::operator= (const Point& pt) {
N= pt.N;
if(Pos == NULL) Pos = new double[N];
memcpy(Pos, pt.Pos, N*sizeof(double));

[Code] ....

The add operator "+" is:
Point operator+( const Point& pt1, const Point& pt2 ) {
Point ptr = Point(pt); // this is a constructor
for (int i=0; i<pt1.N; i++) ptr.Pos[i] += pt2.Pos[i];
return ptr;
}

Based on the above overloading, What I am going to do is :

P = alpha*P1 + beta*P2; // alpha and beta are double constants, P1 and P2 are Points objes

It is ok with Intel C++ 14.0 compiler, but does not work with the microsoft visual c++ 2012 compiler in debug mode in visual studio 2012.

I stepped in those operators and found that visual c++ compiler deconstructs the ptr in operators "*" and "+" before its return while intel c++ finished the operation P = alpha*P1 + beta*P2; and delete those ptrs at last.

Portability of my operator overloading is worse. How to get those arithmetic operators overloading for class with pointers in it.

View 3 Replies View Related

Visual C++ :: Class Fraction - Overloading Operators

Sep 25, 2013

the question am having problems with..

1.Write a class function that defines adding, subtracting, multiplying and dividing fractions by overloading standard operators for the operations.

2. Write a function member for reducing factors and overload I/O operators to input and output fractions. how would i set this up?

View 5 Replies View Related

C++ :: Overloading Binary And Assignment Operators In Vector Class

Feb 5, 2013

I am making a vector class and am having some problems creating the overloaded arithmetic operators and assignment operators.

Here is an example of my "+=" code as it stands the errors are similar/the same for the other operators except "=" operator which works fine:

Vector3& Vector3::operator+=(const Vector3 &rhs) {
Vector3 tmp = *this;
Set(tmp.getX() + rhs.getX(), tmp.getY() + rhs.getY(), tmp.getZ() + rhs.getZ());
return *this;
}

I have tried a lot of different approaches ad always get the error:

error: passing 'const Vector3' as 'this' argument of 'double Vector3::getX()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getY()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getZ()' discards qualifiers

View 5 Replies View Related

C/C++ :: Overloading Functions With Different Types (char / Int)

Apr 24, 2014

For my project, my professor is going to test each of our functions. And the functions of type string, he is going to input a value from 1-1 billion(from our calling program) and it should work. However it is not working. I tried putting in a value greater than 9, but it only prints out 1 + x. It should print whatever value I decided to input.

// COverloadingProj.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "OverLoadingHeader.h"

[Code].....

View 5 Replies View Related

C++ ::  Common Datatype (Char And Int) Operator Overloading

Aug 11, 2014

How you would overload an operator for Common Data-types like "char" and "int".

I often use bool arrays to create a multilevel-trigger-systems, when iterating over multiple containers or waiting for two events to occur at the same time.

For example:
I would define..
bool trigger[2] = {0, 0};

And when doing work via a loop, I use it like so:
while(trigger[0] != 1 && trigger[1] != 1)

You can probably see where I'm going with this. I want to be able to use my bool array with the "!" operator.

So if "trigger == 0" (as a whole), it returns false.

How can I achieve this?

Can you create custom operators? Say if I wanted to create "or-gates" or "xor-gates" etc.

View 3 Replies View Related

C# :: Calculator With Buttons - When Click Numbers Or Operators It Goes To String Array

Mar 26, 2012

I have a calculator with buttons, and when I click the numbers or operators, it goes to a string array. So;

Press 3
Press +
Press 2
Press =

and my array is str[0]='3' str[1]=.... so on...

And also I have a C code which calculates the string you entered with some algorhytms and gives a double value as a result. So the question is;

How can I send my STR array in C# to my C program? I mean I want to use my C program as a source code. and How to return that double value to my C# program?

For now, I don't want to change my C codes into C# codes. I want it to keep as is. Just use it as a source code. Is that possible?

View 2 Replies View Related

C/C++ :: String Concatenation Using Operator Overloading Function

Sep 2, 2014

string concatenation using operator overloading function using + operator. i wrote the proto pls explain body of the function.

class A
{
char *p1,*p2;
public:
A& operator +(A &obj1)
{
-----
-----
}
};
/>

View 3 Replies View Related

C++ :: Count String In A String Using Datatype Char

Dec 20, 2014

I have the codes for such a problem where, to create a program that counts how many times the second string appears on the first string. Yes it counts if you put 1 letter only, but if you put 2, it is an error. As an example. If the first string is Harry Partear, and the second string is ar, it must count as 3. Here's the code:

Code:

#include <iostream>
#include <conio.h>
using namespace std;
int main ()

[Code] ....

View 6 Replies View Related

C++ :: Char Array To String - String Becomes Garbage

Apr 20, 2013

I'm trying to "tokenize" a string using std::string functions, but I stored the text in a char array. When I try to convert it to a string, the string has the first character right but the rest is garbage.

// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessage(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);

[Code] ....

View 3 Replies View Related

C++ :: String Class That Finds A Char In String?

Feb 5, 2013

A string class that finds a char in the string?

View 1 Replies View Related

C++ :: Getting A Char From A String

Apr 5, 2013

I have this problem that I don't know how to sort out. I have a feeling it's really simple

#include <iostream>
#include <string>
void main () {
char test[10] = "gorilla";
char try = test[1];
cout << try;
}

I get the error:
lab4.cpp:6: error: expected primary-expression before "char"
lab4.cpp:6: error: expected `;' before "char""

View 2 Replies View Related

C :: How To Get ASCII Value Of A Char In A String

Mar 4, 2013

how can I find ASCII value (0..255) of a character char in a string?

I have an array of char:

Code: char myarray[50]; and I need to have ASCII value of character eg myarray[10]. How can I do that?

View 1 Replies View Related

C++ :: Char Array To String

Oct 19, 2013

I have some code:

char cHomeTeamFaceOffsPercentageWon[100];
memcpy(cHomeTeamFaceOffsPercentageWon,cSqlHomeTeamFaceOffsPercentageWon,100);

After this, for example, cHomeTeamFaceOffsPercentageWon is, "29%".

Then, I use

std::string szwPercentageWon = std::string(cHomeTeamFaceOffsPercentageWon);

szwPercentageWon is then, "2". Shouldn't this convert correctly, to "29%" as well.

Or is there something I'm missing? Maybe the termination character, or something.

View 1 Replies View Related

C++ :: Convert String To Char

Apr 30, 2013

Is there anyway to convert std::string to char*?

Like:

std::string x="hello world";
char* y=x;

View 6 Replies View Related

C++ :: How To Transform A String Into A Char

Feb 8, 2015

I have had quite a head spinner on trying to transform a string into a char. I've been trying to do this for the project below.

[URL] ....

std::string str = "string";
const char* chr = str.c_str();
cout << *chr << endl;

Above is the code I have tried using and it stores data under *chr, it however only stores one letter rather than the entire word like for example string.

View 2 Replies View Related

C++ :: Convert String Of Hex To Char

Jul 5, 2013

I have this code working:

char tmp_wku[3];
tmp_wku[0]=0x01;
tmp_wku[1]=0x9D;
tmp_wku[2]=0x62;
char tmp_com[11];

[Code] ....

This sends the buffer to a LIN modem. My question is: can this be done better. If I have a astring of hex numbers like "09 98 88 55 42 FF 00 00 FF BD 89". How could I send this without manually makng a char with hex numbers?

View 1 Replies View Related

C++ :: Replace Char In String

May 7, 2013

Is there a way to replace 3 char's in a string. For example i have a string containing

s = "The school is called 7x8"

I want to replace the "7x8" with "LSU". But the "x" can be any letter or number.

Is there a way to do that ?

View 6 Replies View Related

C++ :: Remove Char From String?

Aug 9, 2013

I've to do a function that removes char(input by user) from string , only if it appeared and to reduce spaces.

for instance: string = abcabcd ; and the char= c/
the return string is : ababd

void removeChar(char string2[SIZE], char ch) {
//---NOTE--- its not completely works - there is a problem i.
int read = 0, write = 0;

[Code].....

View 6 Replies View Related

C++ :: Conversion Between Char And String

Jun 7, 2013

I have a question on conversion between char & string. I have cut & pasted the part of the code from my C++ code and my function "decryptPwd" uses C style code with "char" etc.

I need to pass a string (mypwd) somehow to this function after conversion & then compare it to another string (newmypwd).

I tried - "decryptPwd(mypwd.c_str())==newmypwd.c_str()" but it did not work.

..
#include <string>
..
char* decryptPwd(char hash[64]);
main () {
string mypwd;
string newmypwd;
if (decryptPwd(mypwd)==newmypwd)

[Code] ...

View 7 Replies View Related







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