C++;运算符重载<&书信电报;带向量 嘿,我是C++新手,这个操作符有问题:(在StAccess中也是新的)

C++;运算符重载<&书信电报;带向量 嘿,我是C++新手,这个操作符有问题:(在StAccess中也是新的),c++,vector,operator-overloading,operators,C++,Vector,Operator Overloading,Operators,这是我的类测试列表: class TestList{ public: TestList() : listItems(10), position(0){}; TestList(int k) : listItems(k), position(0){}; int listItems; int position; std::vector<int> arr; }; //my current operator is: What should be ch

这是我的类测试列表:

class TestList{
public:
    TestList() : listItems(10), position(0){};
    TestList(int k) : listItems(k), position(0){};
    int listItems;
    int position;
    std::vector<int> arr;
};


//my current operator is: What should be changed?
ostream& operator <<(ostream&, const TestList& tlist, int input){
    os << tlist.arr.push_back(input);
    return os;
}
//

int main() {
testList testlist(5);
 testlist << 1 << 2 << 3; //how should I overload the operator to add these number to testlist.arr ?
 return 0;
}
类测试列表{
公众:
TestList():listItems(10),位置(0){};
TestList(intk):列表项(k),位置(0){};
国际清单项目;
内部位置;
std::载体arr;
};
//我现在的操作员是:应该更改什么?

ostream&operator我想你指的是以下内容

TestList & operator <<( TestList &tlist , int input )
{
    tlist.arr.push_back( input );
    return tlist;
}
你甚至可以用写作来代替这两条语句

testlist << 1 << 2 << 3;

std::cout << testlist << '\n';
应该有

TestList testlist(5);

其他答案完全正确,我只想说一些关于
运营商的一般性问题。您的运营商是否收到了
std::ostream&
?那么返回类型和当前主体似乎不合适。从那开始。既然
接线员感谢您的回答。它帮了我的忙我现在明白了!:D
testlist << 1 << 2 << 3;

std::cout << testlist << '\n';
std::cout << ( testlist << 1 << 2 << 3 ) << '\n';
testList testlist(5);
TestList testlist(5);