код:
std::cout << "enter file to write\n> ";
std::string filename;
std::cin >> filename;
std::ofstream myfile(filename, std::ofstream::app | std::ofstream::binary);
if (myfile){
std::streamsize size = sizeof(city);
myfile.write(temp, size);
std::cout << "SAVED!\n";
}
myfile.close();
поля структуры city:
struct city
{
char name[32];
int citizens;
float territory;
int startyear;
int schools;
};
ошибка:
test.cpp: In function ‘int main()’:
test.cpp:61:36: error: no matching function for call to ‘std::basic_ofstream<char>::write(city&, std::streamsize&)’
myfile.write(temp, size);
^
In file included from /usr/include/c++/7/ostream:693:0,
from /usr/include/c++/7/iostream:39,
from test.cpp:2:
/usr/include/c++/7/bits/ostream.tcc:182:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::streamsize = long int]
basic_ostream<_CharT, _Traits>::
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/ostream.tcc:182:5: note: no known conversion for argument 1 from ‘city’ to ‘const char*’
В данном случае важны не поля структуры, а что вы передаете. Подозреваю, что tmp - не адрес объекта city, а ссылка на него.
В то время как функция ожидает указателя на char.
Сборка персонального компьютера от Artline: умный выбор для современных пользователей