Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:07+00:00 2026-06-13T08:05:07+00:00

I am trying to make a class in order to overload + , –

  • 0

I am trying to make a class in order to overload +,-,*, /, <<, >>, and == in order to work for complex numbers. While trying to troubleshoot, I get multiple error messages saying:

error: ‘double complexType::imaginaryPart’ is private

My header file is

#ifndef COMPLEXTYPE_H
#define COMPLEXTYPE_H
#include <iostream>
#include <cmath>
#include <fstream>

class complexType
  {
    friend ostream& operator << (ostream &os, const complexType &a)
    friend istream& operator >> (istream &is, const complexType &a)
   public:
     complexType();
     complexType(double real, double imag );
     void getComplexType(double&, double&);
     void setComplextType(const double&, const double&);
     friend bool operator == (const complexType &otherComplex) const;
     friend complexType operator + (const complexType &, const complexType &);
     friend complexType operator - (const complexType &, const complexType &);
     friend complexType operator * (const complexType &, const complexType &);
     friend complexType operator / (const complexType &, const complexType &);
    private:
     double realPart; //Variable to store the real part
     double imaginaryPart; //Variable to store the private part
};
#endif // COMPLEXTYPE_H 

and my implementation file is

   #include "complexType.h"

   complexType::complexType()
   {
     realPart = 0.0;
     imaginaryPart = 0.0;
   }

   complexType::complexType(double r, double i)
   {
      realPart = r;
      imaginaryPart = i;
   }

   void setComplextType(double r, double i)
   {
     realPart = r;
     imaginaryPart = i;
   } 

   bool operator == (const complexType &a, const complexType &b)
   {
     return (a.realPart == b.realPart && a.imaginaryPart == b.imaginaryPart);
   }

     operator + (const complexType &a, const complexType &b) 
   {
     complexType temp;
     temp.realPart = a.realPart + b.realPart;
     temp.imaginaryPart = a.imaginaryPart + b.imaginaryPart;
     return temp;
    }

     operator - (const complexType &a, const complexType &b)
     {
        complexType temp;
        temp.realPart = a.realPart - b.realPart;
        temp.imaginaryPart = a.imaginaryPart - b.imaginaryPart;
        return temp;
     }

     operator * (const complexType &a, const complexType &b)
     {
        complexType temp;
        temp.realPart = a.realPart * b.realPart - a.imaginaryPart * b.imaginaryPart;
        temp.imaginaryPart = a.realPart * b.imaginaryPart + a.imaginaryPart *            b.realpart;
       return temp;
       }


       operator / (const complexType &a, const complexType &b)
      {
       complexType temp;
        temp.realPart = (a.realPart * b.realPart + a.imaginaryPart * b.imaginaryPart) /           (pow(b.realPart,2) + pow(b.imaginaryPart,2));
          temp.imaginaryPart = (-a.realPart * b.imaginaryPart +                      a.imaginaryPart*b.realPart) / (pow(b.realPart,2) + pow(b.imaginaryPart,2));
        return temp;
       }

       ostream & operator << (ostream &os, const complexType &a)
       {
         os << "(" << a.realPart << " ," << a.imaginaryPart << "i)";
         return os;
       }

         istream & operator >> (istream &is, const complexType &a)
        {
        char ch, ch2, ch3;
        is >> ch;
        is >> a.realPart;
        is >> ch2;
        is >> a.imaginaryPart;
        is >> ch3;
        return is;
        }

I would greatly appreciate any help, both with the code and with formatting, as this is my first question. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:05:08+00:00Added an answer on June 13, 2026 at 8:05 am

    You have forgotten to prefix your operator definitions with a return type (complexType). So the return type defaults to int and doesn’t match the friend declaration.

    Also, your definition of setComplextType does not have the class prefix (complexType::) and has the wrong parameter types, but that shouldn’t cause the error message you are seeing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a repository class that has a method to order the
I'm trying to make will_paginate's :order to work with this array: @posts = current_user.subscribed_tags.map(&:posts).flatten.paginate(:page
I'm having a bit of trouble trying to get class association's to work correctly.
I am trying to make a class that will represent two different behaivours at
I'm trying to make a class where I put a key and value into
I´m trying to make a custom component to javafx, so I make my class
I'm trying to make spreadsheet with class/method modifiers. The spreadsheet itself can be located
I've been trying to make a generic class to represent a range of values
I was trying to make my own class for currencies using longs, but apparently
I am trying to make a standard class library in vs11 with more available

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.