在尝试重载运算符>>'时出现错误和'<<<

时间:2017-04-01 02:16:15

标签: c++ compiler-errors

错误是“使用重载运算符'>>'很暧昧。“我该如何解决?我一直试图解决这个问题超过一个小时了。该类是'Seq1'(序列的缩写)。

以下是我的函数声明:

  package zk;

 import java.util.Arrays;
 public class Class {

 public boolean isNonStopWord(String x, String[] y) {

    if (Arrays.asList(y).contains(x)) {
        return false;
    }
    return true;
}

public static void main(String [] args) {
    Class cs = new Class();

    String zach = ("Amazon offered up more answers Thursday about what"
            + " caused a bunch of websites to fail two days ago. According "
            + "to a postmortem by the company's cloud services business, "
            + "around 9:37 a.m. PT Tuesday an Amazon worker incorrectly"
            + " punched in a command while trying to debug an issue. "
            + "That command shut down a large set of servers at Amazon Web "
            + "Services' Northern Virginia site, causing a domino effect of"
            + " problems. Other services that relied on those S3 cloud"
            + " storage servers were disrupted. Also, removing so much "
            + "server capacity required a full system restart, which then "
            + "took longer than expected, AWS said. The sites affected "
            + "included Quora, Imgur, IFTTT, Giphy and Slack. Amazon was "
            + "able to fix the issue by about 2 p.m. PT.");

    String [] skoal = {"THE", "BE", "TO", "OF", "AND", "A", "IN",
            "THAT", "I", "IT", "ON", "IN", "BUT", "IS", "WITH"};



    zach = zach.replace(",","");
    zach = zach.replace(".","");
    zach = zach.toUpperCase();
    String [] strings = zach.split(" ");

int july = 0;



    for (String s1: strings)
    {
        boolean answer = cs.isNonStopWord(s1,skoal);
        System.out.println((s1) + "  " + (answer));
        if(answer==false){
            july++;
    }
  }


    System.out.println("There are " + july +" " + "non-stop words");
    zach = "...";           

 }

以下是整个定义:

//Declaration of '<<' operator
std::ostream& operator<<(std::ostream &out, Seq1 &a){

    for(int i = 0; i < a.size();i++){
        out << a.dArray[i] << ' ';
    }
    out << endl;
    return out;
}


//Declaration of '>>' operator
std::istream& operator>>(std::istream &in, Seq1 &b){

    int newSize;

    in >> newSize;
    int *newArray = new int[newSize];
    for(int i = 0; i < newSize; i++){
        std::cout << "Enter in new element of array" << endl;
        in >> newArray[i];
        std::cout << endl;
    }
    b.resize(newSize);
    b.dArray = newArray;

    return in;

}

错误发生在main函数中,该函数与上面的代码在同一个文件中。这些行是“cin&lt;&lt; A0;” 和“cout&lt;&lt; A0;”

//
//  main.cpp
//  AssignmentThree
//
//  Created by William Newman on 3/26/17.
//  Copyright © 2017 William Newman. All rights reserved.
//








#include<iostream>
#include<algorithm>
#include<cassert>
#include "Seq.h"

using namespace SeqWill;

namespace SeqWill{
//Constructors and Destructors
    Seq1::Seq1(int init)
    {
        dArray = new int[init];
        capacity = init;
        used = 0;
    }

   Seq1::Seq1(const Seq1& Source){
        dArray = new int[Source.capacity];
        capacity = Source.capacity;
        used = 0;

    }

   Seq1::~Seq1( )
    {
        delete[] dArray;
    }

    //Accessors

    //Definition of size function
    int Seq1::size(){
        return used;
    }

    //Modification Member Functions


    //Definition of erase_First
    void Seq1::erase_First(int val){
        int *newArray = new int[used - 1];
        int howMany = 0; //makes sure only one instance of the value is removed
        int a = 0; // index of newArray

        for(int i = 0; i < used; i++){
            if(dArray[i] == val && howMany == 0){//Avoids copying first instance of val into new array
                howMany++;
                a--;
            }
            else{
                newArray[a] = dArray[i]; //copies members of dArray into new array
            }
            a++;
        }
        used = used - 1;
        delete[] dArray;
        dArray = newArray;

    }


    //Definition of erase_First
    void Seq1::erase_Last(int val){
        int *newArray = new int[used - 1]; //new array with 1 less member
        int indexOfLast = 0; //keeps track of where last instance of value is
        int a = 0; //index of newArray

        for(int i = 0; i < used; i++){ //counts through entire array saving the index of the last instance of the value in indexOfLast
            if(dArray[i] ==val){
                indexOfLast = i;
            }
        }


        for(int i = 0; i < used; i++){

            if(i == indexOfLast){ // if i equals indexOfLast then function doesn't copy it into new array
                --a;
            }
            else{
            newArray[a] = dArray[i]; //copies each member of dArray that isnt last instance into new array
            }
            a++;
        }

        used = used - 1;
        dArray = newArray;
    }


    //Definition of erase_Occurrence
    void Seq1::erase_Occurence(int occ, int val){
        int *newArray = new int[used - 1]; //new array with 1 less member
        int a = 0; //index of newArray

        for(int i = 0; i < used; i++){
            if(dArray[i] == val && i == occ){ // occurrence is not copied into newArray
                a--;

            }
            else{
                newArray[a] = dArray[i]; //if not occurrence then copied into newArray
            }
            a++;
        }

        used = used - 1;
        dArray = newArray;
    }


    //Definition of erase_From
    void Seq1::erase_From(int ind){
        int *newArray = new int[used - 1];//new array with 1 less member
        int a = 0; //index of newArray

        for(int i = 0; i < used; i++){
            if(i == ind){ //if i == given index then...
                a--; // ...function subtracts from newArray index so as to avoid skipping over location

            }
            else{
                newArray[a] = dArray[i];
            }
            a++;
        }
        used = used - 1;
        dArray = newArray;


    }

    //Definition of insert
    void Seq1::insert(int val){
        dArray[used] = val; //inserts given value at end of dArray
        used++;

    }


    //Definition of insert_First
    void Seq1::insert_First(int val){
        int *newArray = new int[used+1]; //creates new array with one more spot
        used++;
        int a = 0;

        for(int i = 0; i < used; i++){
            if(i == 0){
                newArray[i] = val;
        }
            else{
                newArray[i] = dArray[a];
                a++;
            }

            dArray = newArray;
        }
    }


    //Definition of insert_At
    void Seq1::insert_At(int val, int ind){
        int *newArray = new int[used+1];
        used++;
        int a = 0;

        for(int i = 0; i < used; i++){
            if(i == ind){
                newArray[i] = val;
            }
            else{
                newArray[i] = dArray[a];
                a++;
            }
        }

        dArray = newArray;

    }

    //Definition of resized
    void Seq1::resize(int usedNew){
        used = usedNew;
        int *newArray = new int[used];
        dArray = newArray;
    }



    //Definition of Operator '+'
    Seq1 Seq1:: operator+(Seq1 &b){
        int *newArray = new int[this->used + b.used];
        int a = 0;

        for(int i = 0; i < b.used; i++){
            newArray[a] = b.dArray[i];
        }

        for(int i = 0; i < this->used; i++){
            newArray[a] = this->dArray[i];
        }

        Seq1 A(0);
        A.dArray = newArray;

        return A;
    }

    Seq1 Seq1:: operator+=(Seq1 &b){
        int *newArray = new int[this->used + b.used];
        int a = 0;

        for(int i = 0; i < b.used; i++){
            newArray[a] = b.dArray[i];
        }

        for(int i = 0; i < this->used; i++){
            newArray[a] = this->dArray[i];
        }
        this->dArray = newArray;
        return *this;
    }

    Seq1 Seq1:: operator=(Seq1 &b){
        this->dArray = b.dArray;
        this->used = b.used;
        return *this;

    }

    Seq1 Seq1:: operator==(Seq1 &b){
        if(this->dArray==b.dArray && this->used== b.used){
            return true;
        }
        else return false;
    }


}


//Declaration of '<<' operator
std::ostream& operator<<(std::ostream &out, Seq1 &a){

    for(int i = 0; i < a.size();i++){
        out << a.dArray[i] << ' ';
    }
    out << endl;
    return out;
}


//Declaration of '>>' operator
std::istream& operator>>(std::istream &in, Seq1 &b){

    int newSize;

    in >> newSize;
    int *newArray = new int[newSize];
    for(int i = 0; i < newSize; i++){
        std::cout << "Enter in new element of array" << endl;
        in >> newArray[i];
        std::cout << endl;
    }
    b.resize(newSize);
    b.dArray = newArray;

    return in;

}

0 个答案:

没有答案