C++ ::  assigning To A Member From Struct Obtained By Overloaded Operator

Jan 27, 2014

I'm trying to assign a value to a member of a struct that I called via an overloaded [] operator. I have the following code for the struct:

typedef struct {
float r, g, b, a;
float operator [](int pos) {
switch (pos) {

[Code] ....

And what I wish to do is

MyStruct a;
a[0] = 0.5;

Is it possible with a struct? How to express this to search engines so I haven't been able to find anything about it. If this is not possible with a struct, is there a way to define something that can do all the following things:

SomeStruct test = {0.5, 0.5, 0.5, 1};
test.g = 1.0;
test[0] = 0.0; // test[0] would be equivalent to calling test.r
float somevalue = test[3]; // test[3] would be equivalent to calling test.a

I hope I've been sufficiently clear.

View 2 Replies


ADVERTISEMENT

C++ :: Why Can't Operator Be Overloaded As A Member Function

Apr 3, 2013

why can't << operator be overloaded as a member function is it because that is the way c++ is written and you just can't or is there another reason because I'm confused.

View 2 Replies View Related

C/C++ :: Assigning Value From Struct Member To Outside Structure

Jun 20, 2013

I have the code bellow, and i print the data but cannot assign it to global variable.

struct frequency_data {
 char frequency[10];
 int32 frequenca_mesatare;
 int16 PWM_F;
 int32 PR2;

[Code] ....

View 5 Replies View Related

C++ :: Call To Member Function X Is Ambiguous - Overloaded Member From Header File

Feb 23, 2014

I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file. I have made sure to include the header file and have tried to access it through an object.

This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:

RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous'

This is the code inside randomiser.h:

#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class RandomG {

[Code] ....

This is the error inside xcode: [URL] ....

I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?

I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.

View 3 Replies View Related

C++ :: Type Conversions In Overloaded Operator

Dec 2, 2014

What is another way I could convert string to int in this overloaded operator? This way gives me an error.

Code:
istream &operator>>(istream& in, MasterData& d) {
string value;
getline(in, d.playerId, ',');
getline(in, d.firstName, ',');
getline(in, d.lastName, ',');

[Code] .....

View 4 Replies View Related

C++ :: Overloaded Operator Function In Main

Sep 17, 2013

I don't exactly know how to test my ==friend function in my main.

Here is my .h file:

#include<iostream>
using namespace std;
class Car{
public:
Car();
Car(int yer, string mke);

[Code] ....

View 3 Replies View Related

C++ :: Using Ternary Operator With Overloaded Constructor

Jun 27, 2014

I have two possible questions; can you use a ternary operator to initialize objects with overloaded constructors like

class thing
{
int x;
int y;

[Code].....

I can get around it if I need to but I'd like to learn more about the ternary operator if I can, since I couldn't find anything online that addressed this particular issue, at least in a way I could detect.

View 4 Replies View Related

C/C++ :: Overloaded Operator Killing A Pointer?

Mar 29, 2015

I'm using some overloaded operators (addition, subtraction and variants of) in part of my final major project and, when coming to test it, I've noted that they appear to be killing my pointers eventually.

I say pointers, it's always the same one. But I have isolated it to being the operators. The only two I'm really using are += and -=, though I've defined the others for consistency.

Either A ) what it is I've done wrong (if I have) or B ) why I would see this behaviour. Or, you know, if there's something glaringly obviously wrong with the code that I'm glossing over.

Code is as follows

#pragma once
#include "stdafx.h"
namespace gunpei {
/**
A paired register in the form of r1r2
Enables using two separate arrays for register processing
provides logic for assembling pair and breaking back into individual registers
*/
class GBPairedRegister {

[code]....

View 4 Replies View Related

C++ ::  Overloaded Assignment Operator For Class Template Objects?

May 23, 2013

I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.

Here is the class definition:

template <class dataType>
class myArray {
public:
void setArrayData();

[code]....

And here is the definition of the overloaded assignment operator:

template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) {
int i;
if(this != &rightArray) {
delete [] arrayPtr;

[Code] ....

And here is my main function that tests the operations on objects of the class:

int main(){
//object declarations
myArray<double> list(5); //a single-parameter object declaration of class myArray
myArray<double> myList(2,13); //a two-parameter object declaration of class myArray

[code]....

The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!

View 19 Replies View Related

C++ :: Overriding Of Overloaded Virtual Member

Feb 5, 2013

I've got the following code with output. I can't figure out myself why it's what printed out there. I believe, it has something to deal with overloading/overriding/virtual functions implementations in C++:

class Base{
public: virtual void f(int);
virtual void f(double);
}

[Code].....

Thus here're my conclusions:
1) in line
d.f(1.0);
for some reason compiler preferred casting double->int of the argument and then call to 'Derived::f(int)'.

2)in line
pb->f(1.0);
for some reason compiler preferred call to 'Base::f(double);'. 'Base' is static type of pb, but the dynamic type is 'Derived'.

I believe the answer has to deal with the fact whether virtual table contains in addition to functions' names also the types of arguments they accept. AFAIK, vTable doesn't include such info.

View 7 Replies View Related

C++ :: Extra Parameter Passing To Overloaded Binary Operator Function

Jun 11, 2013

I have a class matrixType that has some overloaded operators (+, -, *, and <<). With a view to having clearly-delineated, perfectly-formatted, four-sided matrices, as shown below:

A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9

and NOT this jagged ones shown below:

A = 1 2 3
4 5 6
7 8 9

or

A + B = 1 2 3
4 5 6
7 8 9
,

I want a scheme in which the string literals (A, A+B, etc.) could be passed as parameters to the overloaded stream insertion (<<) operator function so that I could use the string’s length to determine how much offset from the display screen’s left to apply to each matrix’s row (by using the setw() function). However, I do know that the << operator is a binary operator, meaning the function cannot take more than two parameters: that is what compounds my problem!

View 10 Replies View Related

C++ :: Assigning A Function Using Boolean Operator

Nov 18, 2013

I am writing a code where I have to find out the spot a letter is in. I am getting an error with assigning a function using a boolean operator.

bool is_member(const vector<char> & list, char character) {
for(int i=0; i < list.size(); i++) {
if(character==list[i]) {
return(true);

[Code] .....

My constant vector list is { 'a', 'e', 'i', 'o', 'u', 'y'}. My error comes in on line 20. I am not calling the boolean correctly. If I type in the letter "i". Then the function should output 2 since i is in the 2nd index spot of my vector list. How to fix my error? I am not understanding why my line of code is not working.

View 10 Replies View Related

C++ :: Assigning Value To Struct Variables?

Jul 29, 2013

I'm trying to assign a value to the variables in my struct using the statement:

fscanf(fp, "%s%10s%9s%s", acct.name, acct.num, acct.pin, strbal)

but when I try to access them, it looks like the variables (acct.name, acct.num, acct.pin) are all empty.

Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXAMT 999999.99

[Code].....

View 14 Replies View Related

C++ :: Error On Assigning New Char Value From Struct

Feb 17, 2015

Well I tried to assign a new char value from a struct to another char variable but I got the "Cannot convert 'int' to 'char'" error when compiling. I've tried several alternations but I still can't get away with this error.

Here's a section of the code:

javascript:tx('code')
for(int i = 0 ; i < 10 ; i++){
pts[i].dist = sqrt((pts[i].x*pts[i].x)+(pts[i].y*pts[i].y));
}

[Code].....

View 1 Replies View Related

C++ :: Overloaded Operator Defined In Header File - Gives Error In CPP File Of Class

Apr 12, 2014

I am working on an assignment in which i have to perform th following task

myClass itsObject1,itsObject2;
itsObject2=5000+itsObject1;

I have defined overloaded operator as follows in the header file but in the cpp file of the class it gives error.

friend vli &vli::operator + (int &a,vli &obj);

How to define it in cpp file of my class?

View 1 Replies View Related

C++ :: Assignment Operator But With Some Member Exceptions

Jan 9, 2015

The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':

#include <iostream>
#include <string>
struct Person {
std::string name;
int ID, age, height, weight;

[Code] .....

Name = Bob
ID = 2047
Age = 38
Height = 183
Weight = 170

Name = Frank
ID = 5025
Age = 25
Height = 190
Weight = 205

Bob pretends to be Frank, but keeps his name and ID.

Name = Bob
ID = 2047
Age = 25
Height = 190
Weight = 205

But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.

View 3 Replies View Related

C/C++ :: Binary Operator Overloading Using Member Function

Dec 26, 2014

I want to create a program that shows the total of 2 subjects & percentage of student using binary operator overloading with member function in C++.

View 10 Replies View Related

C :: Initialize Data Member In Struct

Mar 28, 2013

I was looking at some linked list material and was wondering something. Can you initialize a data member inside a struct like in C++? i.e.

Code:
typedef struct node
{
int data;
struct node * next = NULL; // this is the line in question
} LLnode;

View 3 Replies View Related

C :: Cannot Assign Struct Member Values

Feb 13, 2015

I'm a C beginner trying to assign struct member values to other struct members to create a list.

I've tried dot notation, pointer notation, strcpy, memcpy, memmove and normal assignment.

Nothing has worked.

cust_list.h

View 5 Replies View Related

C++ ::  Static Member In Struct Array?

Jun 25, 2013

Here's the definition of my struct:

struct Speaker {
static int numElem;
string name;
int number; // Phone
string topic;
float fee;
};

// IN main() FUNCTION
Speaker s[10];

The goal is for numElem to keep track of how many of the 10 elements are in use. However, I'm not sure the proper way to access the element, if it's even possible.

View 7 Replies View Related

C/C++ :: Segfault When Accessing Member Of A Struct

Mar 18, 2014

The program I'm working on is a very basic relational database. I've isolated my problem for simplicity. I get a segfault right here when I try to access db->relationCount. I tried printing db->relationCount from within loadDB and that worked,

[code]
loadDB(db, configFile);
printf("%d",db->relationCount);
fflush(stdout);

View 5 Replies View Related

C++ :: Write A Program To Store Marks Obtained By 50 Students?

Feb 28, 2014

Q.1 Write a program to store marks obtained by 50 students. Initialise the array having passed to a function initilise() having passed the array as parameter. Write another function which should take this array as parameter and return the highest score from the array. you have to write the proper function call, prototype and definition for each function.

And this is the program which i wrote and got errors: -

#include<iostream.h>
#include<conio.h>
#define s 50
void init(int m[s]);
void display(int m[s]);
void find_max(int max, int m[s]);
void main(){
clrscr();
int max;

[code]....

View 1 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

C :: Set Struct Member Variable For Structure Inside Def

Mar 12, 2014

This is with Linux gcc

Code:
typedef struct _a
{
int id;
} a;
typedef struct _b
{
a my_a;
my_a.id = 1; // error: expected specifier-qualifier-list before "my_a"
} b;

I get error: expected specifier-qualifier-list before "my_a"

I must set the id for the kind of struct created inside the struct def because main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different values I will cast to the correct one and know it's members by it's id. How do I ?

View 10 Replies View Related

C++ ::  How To Get Relative Address Of Member Of Class Of Struct

Apr 2, 2014

How to get relative memory address of members of Class or Structure ? I want to auto scan the members of Class/Struct, and show the address/value like the "watch window" in debug mode of popular C/C++ IDE software.

View 2 Replies View Related

C/C++ :: Declaring Constant Struct As A Member Of Class

Oct 2, 2014

I would like to have a unmodifiable standard of WAVEFORMATEX defined as a member of a class of mine. Something like:

class InputTest {
public:
const WAVEFORMATEX StandardWaveFormat;
public:
void TakeInput(WAVEFORMATEX pFormat);
};

Then in my cpp file to hard-code the values:

WAVEFORMATEX InputTest::StandardWaveFormat {
//Instantiate WaveFormat -- PCM standards
StandardWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
StandardWaveFormat.cbSize = 0; //extra information sent over stream. Usually ignored in PCM format.

[Code] ....

I get the following errors starting with the header file:

Error1error C2146: syntax error : missing ';' before identifier 'StandardWaveFormat'
Error2error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

both associated with the "const WAVEFORMATEX StandardWaveFormat; " line.

Here's a link to the WAVEFORMATEX struct: [URL] .....

Then the cpp source code is probably way off. Let me know if you'd like to see the errors associated with that.

View 11 Replies View Related







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