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 8841711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:47:19+00:00 2026-06-14T10:47:19+00:00

I am getting some problems overloading the << operator. The error is this one:

  • 0

I am getting some problems overloading the << operator.
The error is this one: ‘JSON’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’

How is the correct method to overload the operator <<.
My objective is to be able to do std::cout<

My code is:
main.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include "JSON.hpp"

int main()
{
    std::cout<<"JSON V0.1"<<std::endl;

    std::string line;
    std::ifstream file;
    std::stringstream ss;
    JSON obj;

    file.open("test.json");
    if (file.is_open())
    {
        std::cout<<"File opened"<<std::endl;
        ss << file.rdbuf();

        obj.parse(ss);
        file.close();
     }

     std::cout<<obj<<std::endl;

     return 0;
}

JSON.hpp

#ifndef _JSON_H_
#define _JSON_H_

#include <string>
#include <sstream>

#include <boost/property_tree/ptree.hpp>

class JSON
{
    public:
        bool parse(std::stringstream &stream);
        std::string get(std::string &key);

    private:
        boost::property_tree::ptree pt;
};
#endif

JSON.cpp

#include <boost/property_tree/json_parser.hpp>

#include "JSON.hpp"

bool JSON::parse(std::stringstream &stream)
{
    boost::property_tree::read_json(stream, pt);
    return true;
}

std::string JSON::get(std::string &key)
{
    std::string rv = "null";
    return rv;
}

std::ostream& operator<<(std::ostream& out, const JSON& json)
{
    return out << "JSON" << std::endl;
}
  • 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-14T10:47:20+00:00Added an answer on June 14, 2026 at 10:47 am

    The compiler only considers functions that it has seen before when it sees a function call, so it doesn’t see the operator<< you have in the .cpp file.

    You must put a forward declaration of your operator<< somewhere so the compiler knows about it, like in the header:

    std::ostream& operator<<(std::ostream&, const JSON&);
    

    Then the compiler will know about the function when it sees you call it.

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

Sidebar

Related Questions

I'm having some problems getting a UIImage from a CVPixelBuffer . This is what
I am getting some problems while trying to use this regex: (public +function|private +function|function)
I'm having some problems getting the Friend List of a twitter user from R,
I'm using PostgreSQL with CodeIgniter and I'm with some problems getting this query to
I have some problems getting the custom formated JSON data into a dijit.form.FilteringSelect. It
I am having some problems getting this animation to execute. I have a feeling
Having some problems with getting some ajax script to work. Using this as the
I am having some problems getting this layout to work properly in IE (both
I am having some problems getting this code to work properly. I want to
I am having some problems getting this Wordpress nav bar to render correctly in

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.