Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania
Viewing all articles
Browse latest Browse all 65225

Tworzenie szablonu zamiast dwóch bliźniaczych funkcji sprawdzających zapisu i odczytu pliku

$
0
0

W jaki sposób poprawnie stworzyć szablon następujących funkcji?

Czy jest to w ogóle możliwe dla ifstream& oraz ofstream&?

void checkOpening(ifstream& handle);
void checkOpening(ofstream& handle);
void checkOpening(ifstream& handle) {
	if (handle.good())
		cout << "The input file has been opened correct."<< endl << endl;
	else {
		cout << "The file hasn't been opened."<< endl;
		cout << "Application will be closed."<< endl;
		exit (STATUS);
	}
}

void checkOpening(ofstream& handle) {
	if (handle.good())
		cout << "The output file has been opened correct."<< endl << endl;
	else {
		cout << "The file hasn't been opened."<< endl;
		cout << "Application will be closed."<< endl;
		exit (STATUS);
	}
}

Próbowałem użyć poniższego zapisu...

template <typename T>
void checkOpening(T& handle);
template <typename T>
void checkOpening(T& handle) {
	if (handle.good())
		cout << "The file has been opened correct."<< endl << endl;
	else {
		cout << "The file hasn't been opened."<< endl;
		cout << "Application will be closed."<< endl;
		exit (STATUS);
	}
}

Jednak kompilator wyrzuca błędy podczas wywoływania funkcji określonej szablonem o następującej treści:

||=== Build: Debug in 01 Tester (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `main':|
E:\01 [C++]\003\main.cpp|21|undefined reference to `void checkOpening<std::basic_ifstream<char, std::char_traits<char> > >(std::basic_ifstream<char, std::char_traits<char> >&)'|
E:\01 [C++]\003\main.cpp|27|undefined reference to `void checkOpening<std::basic_ifstream<char, std::char_traits<char> > >(std::basic_ifstream<char, std::char_traits<char> >&)'|
E:\01 [C++]\003\main.cpp|33|undefined reference to `void checkOpening<std::basic_ofstream<char, std::char_traits<char> > >(std::basic_ofstream<char, std::char_traits<char> >&)'|
||error: ld returned 1 exit status|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Bardzo proszę o wyjaśnienie, czego tutaj nie rozumiem lub co robię niepoprawnie... frown


Viewing all articles
Browse latest Browse all 65225

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>