1

my code works perfectly in clion. however when I try to compile in visual studio I get the following error: error: base class 'std::__1::ios_base' has private copy constructor.

main.cpp

#include <iostream>
#include <string>
#include <vector>
#include "ReadFile.h"
#include "SudokuPuzzle.h"

/*
 * Write your sudoku program! Do not put all of your code in main.cpp;
 * make new files as necessary.
 *
 * Make sure that the correct .cpp and .h/.hpp files are available to the
 * sudoku and testing executables as necessary.
 * CLion should prompt you to add the right info to the CMakeLists.txt
 * whenever you create new .cpp files.
 */

int main(int argc, char *argv[]) {

    std::cout << "Enter a path to the file of puzzles" << std::endl;
    std::string path;
    getline(cin, path);

    vector<stringstream> puzzles = TextToPuzzle((string &) path);

    for(stringstream &curPuzzle : puzzles) {

       SudokuPuzzle puzzle;
       curPuzzle >> puzzle;
       puzzle.SetArray();
       std::cout << puzzle;


    }

    return EXIT_SUCCESS;
}

readfile.cpp

//
// Created by Ahsan Gilani on 10/20/19.
//
#include "ReadFile.h"

const char* kSpfVersion = "#spf1.0";
const int kNumTiles = 81;



vector<stringstream> TextToPuzzle(string &file_path) {
    vector<stringstream> puzzle_list;
    ifstream file_stream(Trim((const char*) file_path.c_str()));

    if(!file_stream) {
        std::cout << "Failed to read file, unable to find solutions" << std::endl;
        vector<stringstream> empty;
        return empty;
    }
    else {
        std::cout << "File has been read" << std::endl;


        if (!SeperatePuzzles(file_stream, puzzle_list)) {
            vector<stringstream> empty;
            return empty;
        }
    }


    return puzzle_list;
}

bool SeperatePuzzles(ifstream &file_stream, vector<stringstream> &puzzle_list) {

    string line_contents;
    getline(file_stream, line_contents);

    if(Trim(line_contents).compare(kSpfVersion) != 0) {
        std::cout << "This is not a valid SPF file" << std::endl;
        return false;
    }

    while(getline(file_stream, line_contents)) {
        if(ValidPuzzleInput(line_contents)) {
            puzzle_list.push_back(stringstream(Trim(line_contents)));
        }
        else {
            std::cout << "The contents of this file are not valid." << std::endl;
            std::cout << "This line caused an error: " + line_contents << std::endl;
            return false;
        }
    }

    return true;
}

bool ValidPuzzleInput(const string &file_line) {
    if(Trim(file_line).length() != kNumTiles) {
        return false;
    }

    return Trim(file_line).find_first_not_of("123456789_") == string::npos;
}

//method below dervived from: https://stackoverflow.com/questions/25829143/trim-whitespace-from-a-string/25829178
string Trim(const string &str)
{
    size_t first = str.find_first_not_of(' ');
    size_t last = str.find_last_not_of(' ');

    //if there are no characters other than spaces, prevents going out of bounds
    if (first == string::npos) {
        return "";
    }

    return str.substr(first, (last - first + 1));
}


readfile.h

//
// Created by Ahsan Gilani on 10/20/19.
//

#pragma once

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

using namespace std;

vector<stringstream> TextToPuzzle(string &file_path);
bool SeperatePuzzles(ifstream &file_stream, vector<stringstream> &puzzle_list);
bool ValidPuzzleInput(const string &file_line);

//helper method to trim whitespaces from string
string Trim(const string &str);

I've looked all over for solutions, and I'm passing by reference. and Clion's IDE says everything fine. I can't figure out why this isn't working. It seems like the error is being made when TextToPuzzle is called which is in ReadFile.

after looking at comments: this is the error:

Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/streambuf:493:64: error: 
      base class 'std::__1::ios_base' has private copy constructor
_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>)

also the link to see what happens in visual studio https://github.com/ahsang2/sudokusolver

                                                           ^
Ahsan
  • 91
  • 1
  • 5
  • Can you provide info on the compilers each IDE is configured to use? – Romen Oct 24 '19 at 19:11
  • Avoid `using` statements in header files. Also, there is no need to cast the file path to a `std::string&` when calling the `TextToPuzzle`. – Mansoor Oct 24 '19 at 19:12
  • `vector puzzle_list`i is your villain `vector` is storing copies. See if moving the `stringstream`s makes sense. – user4581301 Oct 24 '19 at 19:13
  • @user4581301, We still need to get to the bottom of why CLion can compile it but Visual Studio can't. – Romen Oct 24 '19 at 19:14
  • Clang probably noticed that `puzzle_list.push_back(stringstream(Trim(line_contents)));` is operating on a temporary and decided , "Smurf it. No harm in moving it." – user4581301 Oct 24 '19 at 19:18
  • Wait a sec. private copy constructor. How old is this visual studio? I'd expect that to be a deleted copy constructor,by now, not a private one. Fired the code up in VS 2015 and hacked out the bits about SudokuPuzzle.h. Cannot reproduce. Cannot guarantee this works before that due to lack of C++11 support. – user4581301 Oct 24 '19 at 19:20
  • Flat-out can't reproduce. Can we get the full error message from the Output tab and a [mcve]? – user4581301 Oct 24 '19 at 19:31
  • how can I upgrade visual studio @user4581301 – Ahsan Oct 24 '19 at 22:38
  • also, how do I get info on the compilers, sorry I'm new to this. all I've used is IntelliJ and now im transitioning to c++ and trying to learn this @Romen – Ahsan Oct 24 '19 at 22:38
  • Usually you download a new one. The current edition released is Visual Studio 2019. If this is what you have, unfortunately I can't test against it. I only have 2010 and 2015. – user4581301 Oct 24 '19 at 22:49
  • im on 2019 :/. should I give a link to the GitHub repo with all the code – Ahsan Oct 24 '19 at 22:54
  • here's a link to the code, please try in VSC. https://github.com/ahsang2/sudokusolver – Ahsan Oct 24 '19 at 22:57
  • I find several other interesting problems, check the warnings once you've gotten the error sorted out, but [I cannot produce the reported error](https://godbolt.org/z/1MeQ6G). – user4581301 Oct 24 '19 at 23:27
  • so does the code work correctly for you? @user4581301 – Ahsan Oct 25 '19 at 00:20
  • Didn't run it. Just compiled it. It compiled. Should be a couple warnings that need to be addressed though. Turn on the warnings and crank up the volume loud because warnings are the first line of defense against logic errors. – user4581301 Oct 25 '19 at 00:37

0 Answers0