C++ :: Overloaded Constructor Of String

Sep 2, 2013

I have to implement the following class:

class MyString {
private:
char *str; // Pointer to the char array that holds the string
int strLength; // Variable to store the length of the string

public:
// Default constructor to initialize the string to empty string
MyString();

[Code] .....

How can I define overloaded constructor at line 12.

View 1 Replies


ADVERTISEMENT

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++ :: Why Overloaded Copy Constructor Is Not Getting Called Here

Nov 19, 2012

#include<iostream>
using namespace std;
class Cents {  
public:
  int m_nCents;
   Cents(int nCents=0):m_nCents(nCents){
       cout<<"Calling normal constructor with value:";   m_nCents = nCents;
       cout<<m_nCents<<endl;

[code].....

Question is :Why is the overloaded copy constructor that I have written not getting called here?Internally default copy constructor is getting called.Thats why we get value of obj2.m_nCents as 37.

View 6 Replies View Related

C++ :: Can't Initialize String Array In Constructor

Jul 22, 2013

I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token

Here is my code:

header file (GuessWord.h):
#ifndef GUESSWORD
#define GUESSWORD
#include <string>
using namespace std;

[code].....

View 2 Replies View Related

C++ :: Class Constructor With String Const Pointer As Parameter

Feb 19, 2013

I know my product.cpp constructor is wrong but how can I set my private variables.

//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H

[Code].....

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

C++ :: Calling Constructor From Constructor Set Pointer To Null

Jan 25, 2014

VS 2012 / Windows 7 64 bit

class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}

[Code] ....

it works until the myclass(int* p) calls myclass()

then the MyPointer will become CCCCCCCC (NULL value)!

is there a way to stop the second constructor from resetting the pointer value to null?

View 3 Replies View Related

C++ :: Getline Overloaded Error

Dec 5, 2014

I am currently having trouble to have getline to read line from the file. Error is: "no instance of overloaded function "getline" matches the argument list"

code is as follows:

std::ifstream config("config.txt");
string process[4];
int linecount = 1;
if (config.is_open)
{
while (config.peek() !=EOF)
{
getline(config, process);
linecount++;
}
}

View 5 Replies View Related

C++ :: Using Constructor Within Constructor In Same Class

Feb 28, 2012

I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:

error: type "mainClass" is not a direct base of "glavna"

This is the program I tried:

Code:
class mainClass {
private:
int x,y;

Code] ......

View 6 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++ :: Templates / Lambdas And Overloaded Operators

Feb 16, 2015

What I'm trying to do is create a class for constructing an 'op tree' for parsing infix notation.

I started with a base class that uses a map of lambdas to actually calculate the operations (since they are mostly 1 line functions) of passed in integer or float values.

This base class just uses a templated T type as the lvalue and rvalue. I realized though that if I overload the math operators, +, -, etc.. I could also use the class itself as a type for the lvalue and rvalue. This lead me to think I could easily create the op tree by using Operation class members themselves as operands, which I think makes sense but I'm having some trouble expressing the code.

Heres what I have thus far

Code:
#include <map>
#include <string>
#include <algorithm>
#include <iostream>

namespace Calc {

[Code] .....

Example, if you look at the main() function I create normal operations easily with integer values. I then try to create a "tree" operation that includes 2 sub-operations as it's rvalue and lvalue, that is where I'm having some conceptual problems as far as implementing the code to do that.

View 2 Replies View Related

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++ :: 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++ :: Output Is Printing Twice With Overloaded Operators

Feb 16, 2013

I'm having an issue with output, luckily everything else works!! I'm working with Mixed Numbers and operations on them. So, here's the code I'm testing with:

int main() {
Mixed m1(5), m2(1,1,1), m5(2,2,3);
cout << "m1+m2= " << m1+m2 << endl;
cout << "m1 + 10=" << m1+10 << endl;
cout << "m1 - 10=" << m1-10 << endl;
return 0;
}

And here is the output for that code:

File name: fract.h

#ifndef fract_H
#define fract_H

#include <iostream>
using namespace std;

const int DEFAULT_VAL = 0;

[Code] ....

View 2 Replies View Related

C++ :: Pow - No Overloaded Function Takes 1 Arguments

Oct 4, 2013

I keep getting this error in my code. I believe it is because to use pow(x,y) both x and y have to be double, but how do i put that into my formula under calculations?

#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main() {
// Declaration section: Declaring all variables.

[Code] ....

View 4 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++ :: Calling Overloaded Operators In Main

Jan 10, 2013

I believe I have the syntax correct but I'm having difficulty calling my overloaded == operator in main (last snip-it of code). Below are several files explaining the code.

Commission.h here is where the friend bool operator == exists and I believe I have it initialized correctly.

#ifndef COMMISSION_H_INCLUDED
#define COMMISSION_H_INCLUDED
using namespace std;
class Commission {

public:
Commission();
Commission(int, double, double, double );
~Commission();

[Code] .....

View 3 Replies View Related

C++ :: Getting Error C2678 With Overloaded Functions

Apr 22, 2014

I have 2 header files one contains my personType class which works independently as I created it for a previous lab and a binarySearchTree<Type> which allows you to create and do various functions with binary search tree's like inserting, searching,and deleting. Which also works independently usings numbers and strings. But when I have combined the 2 trying to make a binary tree of type personType I am running into this problem I can't seem to figure how to fix. The entirety of my code is provided for these 2 header files.

Error1error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

PersonType
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include "orderedArrayListType.h"
#include "arrayListType.h"
using namespace std;

[code]....

View 12 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++ :: Net Pay By Pass Referencing A Gross Pay Overloaded Function

Oct 23, 2014

I need to return taxes paid and net pay by pass referencing a gross pay overloaded function. Are the values returned from calling the overloaded function file stream objects? Can they be passed simply through a pass-by-reference function?

//Read Data from File, gather info and calculate pay, output data to file
while(counter < x) {
inFile >> last_name >> first_name >> hours_worked >> hourly_pay;
outFile << first_name << " " << last_name << " ";
outFile << calculate_gross_pay(hours_worked,hourly_pay);
counter++;
outFile<<endl;

[code].....

View 8 Replies View Related

C++ :: Vector Of Classes With Overloaded Operators Not Compiling

Dec 6, 2014

I am working on a school project and am stuck in the debugging. I am compiling with g++ and am using c++11.

Error message

In file included from /usr/include/c++/4.7/algorithm:63:0,
from date.h:7,
from date.cpp:1,
from schedule.h:1,
from schedule.cpp:1,
from date_driver.cpp:1:

[Code] ....

Here are my overloaded operators for my date class.

bool Date :: operator == (Date otherDate){
if (((otherDate.getDay () == getDay ()) && (otherDate.getMonth () == getMonth ())) && (otherDate.getYear () == getYear ()))

[Code] .....

View 1 Replies View Related

C++ :: Map Container - Calling Overloaded Virtual Method

Oct 7, 2013

Im trying to create a map container with the key being an ID number and the value being a pointer to a class object. Currently Im creating objects and storing their address in the container. I am getting a runtime error when calling the virtual method with this pointer. I believe that the problem is being called because they aren't being called pointer/reference. let me know if you need more.

if(command == 'F'){
inputDataFile>>name>>mNumber>>email>>department>>tenure;
faculty newFaculty(name,mNumber,email,department,tenure);
person* facultyAdd = &newFaculty;
cout<<"Note: Adding "<<mNumber<<"..."<<endl<<"Adding ";
people.insert(pair<string,person*>(mNumber,facultyAdd));

[Code]...

View 3 Replies View Related

C/C++ :: Statement Cannot Resolve Address Of Overloaded Function

Feb 11, 2015

#include<iostream>
using namespace std;
#include<string>
main() {
cout<< "donner votre nom:";endl;
string nom("sans nom");
cin>> nom;endl;
cout<< "votre nom est:"<<nom; endl;
return 0;
}

when i try to build this program, i get this masseges:

in function 'int main()':
statement cannot resolve address of overloaded function

this a picture of the errors:

View 6 Replies View Related

C++ :: Derived Class Constructor Using Base Class Constructor?

Jan 1, 2013

Is this example correct? This example from a book

Constructor of the Base Class
Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
name = n;
nationality = nat;
sex = s;
}

Constructor of the Derived Class (inherited from the base class)

Student(char* n, int s=0, char* i=""):
Person(n, s)

Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?

View 5 Replies View Related

C++ :: Ambiguous Call On Overloaded Variadic Template Operators

Oct 30, 2013

When compiling the code

#include "tensor.h"
int main() {
Tensor<2,-2> m = {{1,2},{1,3}};
Tensor<2> v = {1,5};
std::cout<<m*v<<"

[Code] ....

Why do I get an ambiguity and why is not the wanted operator*-overload (the last one in the tensor.h file) not even mentioned as one of the candidates? Is it clear what I want to do? And if so, what can I do to make the call unambiguous?

View 3 Replies View Related

C++ :: Error - Statement Cannot Resolve Address Of Overloaded Function

Nov 2, 2013

I can't seem to figure out whats causing this error: statement cannot resolve address of overloaded function . Error is before line 14 in bubblesortrand function. Thnx in advance.

void bubblesort(int num[], int a_size)
{
int i, j, temp;
for(i = (a_size - 1); i >= 0; i--)

[Code].....

View 4 Replies View Related







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