Experience in debugging cinvec.h (C++ header file definition function)

Writing a header file is really a headache!
Defining functions in header files is not easy!

cinvec.h purposes detailed in my blog about C ++, think multi-line variable number of digital inputs .

The contents of cinvec.h are as follows:

#ifndef CINVEC
#define CINVEC
#include <iostream>
#include <vector>
#include <string>
#include <sstream> // std::istringstream

std::vector<int> cinvec_int(std::string cin_int)
{
    
    
	getline(std::cin, cin_int);
	std::vector<int> vint;
	std::istringstream is(cin_int);
	int i;
	while (is >> i)
		vint.push_back(i);
	return vint;
}

std::vector<double> cinvec_double(std::string cin_double)
{
    
    
	getline(std::cin, cin_double);
	std::vector<double> vdou;
	std::istringstream is(cin_double);
	double i;
	while (is >> i)
		vdou.push_back(i);
	return vdou;
}
#endif // !CINVEC

At first, it could not be successful, because the contents of the two functions defined in parentheses could not be determined. I have always wanted to pass the input directly, so I borrowed the form of getline and used std::istream. However, the header file was okay, and the following error was reported in the source file:

(std::cin) C++ function (declared at line 69 of) cannot be referenced – it is a deleted function

This is a headache. Online search (including search within CSDN), but can not get results.
But at this time, I opened my thoughts and didn't need to give cin a special position like getline. It can be similar to the commonly used "set without seeking" in mathematics, set an intermediate string parameter, and this parameter can be directly replaced in the header with getline. This idea is formed.

Note:

  • 2020.8.28 It took more than an hour to solve the problem, and keep it in the blog.
  • The other header file I wrote is detailed in my blog [Notes] Conversion of base to base in C++ .

[C++ Primer(5th Edition) Exercise] Exercise Program-Chapter5 (Chapter 5)

Guess you like

Origin blog.csdn.net/weixin_50012998/article/details/108285151