我正在为我的 C++ 类介绍项目工作,该项目将构建一个程序来计算各种统计数据。我已经完成了计算,但我们的教授希望我们用来std::istream从文件中收集输入。程序将不断从文件中收集信息,直到到达文件结束标记为止。我对工作方式非常不熟悉,std::istream当我尝试编译时,我一直遇到这个错误。
main.cpp:5:10: 错误:调用没有对象参数的非静态成员函数 stats::getInput(std::cin);
这是我的 stats.cpp 文件:
#include "stats.h"
#include <vector>
#include <cstdlib>
#include <iostream>
stats::stats(){
}
std::vector <double> stats::getInput(std::istream& input_stream){
std::vector <double> stream;
double x;
while(input_stream){
input_stream >> x;
// std::cout << "your list of numbers is: " << x << std::endl;
if(input_stream){
stream.push_back(x);
}
}
return stream;
}
Run Code Online (Sandbox Code Playgroud)
这是我的头文件:
#ifndef _STATS_
#define _STATS_
#include <vector>
#include <cstdlib>
class stats{
public:
stats();
std::vector <double> getInput(std::istream& input_stream);
private:
};
#endif
Run Code Online (Sandbox Code Playgroud)
这是我的 main.cpp 文件:
#include …Run Code Online (Sandbox Code Playgroud) 我正在为我的c ++课做作业,而我似乎无法弄清楚我做错了什么.
以下是方向:
练习:read02
描述
在本练习中,您将创建一个函数来从字符串中获取字符,但前提是指定的索引在范围内.如果超出范围,则返回换行符.
功能名称
read02
参数
str: a std::string
index: a size_t
Run Code Online (Sandbox Code Playgroud)
回报价值
char存储在str中的char,除非index超出范围,然后\n.
例子
std::string data = "hello";
size_t i = 3;
char x = read02(data, i);
Run Code Online (Sandbox Code Playgroud)
提示
字符串文档请记住包含头文件.size_t需要cstdlib头文件.size_t是无符号的(仅包括> = 0的值).'\n'是换行符的字符常量.
这是我有的:
#include <string>
#include <cstdlib>
char read02(std::string str, size_t index){
size_t i;
for( i = 0; i < str.size(); i++){
if(index > 0 && index < str.size()){
return str[index];
}
else{
return '/n';
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
error: multi-character character constant [-Werror=multichar]
return …Run Code Online (Sandbox Code Playgroud)