C++ Error: default argument given for ...

Asked 2 years ago, Updated 2 years ago, 24 views

An error occurs in the write and readline parts of StringWrapper.cpp That part of the assignment was already shown as a prototype, so I put it in as it is.

How can I fix this error? Thank you.

main.cpp
#include <iostream>
#include "ThinArrayWrapper.h"
#include "ArrayWrapper.h"
#include "StringWrapper.h"
#include <stdexcept>

int main(){
    /*
    SuperThinArrayWrapper SuperThinArrayWrapper;

    for (int n = 0; n < 20; n++){
        SuperThinArrayWrapper.array[n] = (n + 1) * 10; };

    SuperThinArrayWrapper.print_array (SuperThinArrayWrapper);

    ThinArrayWrapper ThinArrayWrapper;

    ThinArrayWrapper.fill_array_v0 (ThinArrayWrapper);
    ThinArrayWrapper.print_array (ThinArrayWrapper);

    ThinArrayWrapper.fill_array_v1 (ThinArrayWrapper);
    ThinArrayWrapper.print_array (ThinArrayWrapper);

    ThinArrayWrapper.print_array (ThinArrayWrapper.fill_array_v2(ThinArrayWrapper));
    */

    /*
    ArrayWrapper catch_example_array{20};
    for (int i = 0; i < catch_example_array.get_size(); ++i){
        catch_example_array.set(i, (i+1));
    }
    int selected_index = 0;
    while (selected_index >= 0){
        std::cout << "Enter an index to print the item.  (-1 to stop): ";
        std::cin  >> selected_index;
        if (selected_index != -1){
            try{
                int value = catch_example_array.get (selected_index);
                std::cout << "The item at index " << selected_index
                          << " is " << value << '\n';
            }
            catch (const std::out_of_range&){
                std::cout << "The index " << selected_index 
                          << " is not a valid index.\n";
                std::cout << "Valid indices are 0 to "
                          << catch_example_array.get_size() - 1
                          << '\n';
                // // Reset to allow the loop to continue if index was negative.
                selected_index = 0;  
            }
        }
    }
    */

    char myString[]{ "string" };

    StringWrapper StringWrapper(myString);
    std::cout << StringWrapper.get(1);
    char exampleChar[]{"i"};
    StringWrapper.set(1, exampleChar);
    std::cout << StringWrapper.get(1);



    return 0;
}
StringWrapper.h
#ifndef STRINGWRAPPER_H
#define STRINGWRAPPER_H
#include <iostream>

class StringWrapper{
    public:
        StringWrapper (const char *myString);
        void set (int position, const char *myString);
        char get (int position);
        void write (std::ostream& strm=std::cout) const;
        void readline (std::istream& strm=std::cin, char delim='\n');

    const static int max_capacity = 262144;
    private:
        char myString[40];
};


#endif
StringWrapper.cpp
#include "StringWrapper.h"
#include <cstring>
#include <stdexcept>
#include <iostream>

StringWrapper::StringWrapper (const char *myString){
    strncpy(StringWrapper::myString, myString, sizeof(myString));
}

void StringWrapper::set (int position, const char *myString){
    if ( position > strlen(StringWrapper::myString))
        throw std::out_of_range{"Array index out of bounds."};
    StringWrapper::myString[position] = *myString;
}

char StringWrapper::get (int position){
    if ( position > strlen(StringWrapper::myString))
        throw std::out_of_range{"Array index out of bounds."};
    return StringWrapper::myString[position];
}

void StringWrapper::write (std::ostream& strm=std::cout) const{

}

void StringWrapper::readline (std::istream& strm=std::cin, char delim='\n'){

}

c++

2022-09-20 18:07

1 Answers

The following code in the StringWrapper.cpp file is a problem.

void StringWrapper::write (std::ostream& strm=std::cout) const{

}

void StringWrapper::readline (std::istream& strm=std::cin, char delim='\n'){

}

Please correct the code above as below.

void StringWrapper::write(std::ostream& strm) const {

}

void StringWrapper::readline(std::istream& strm, char delim) {

}


2022-09-20 18:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.