在将多个结构体的读/写大小写入文件时编译错误 [英] Compile errors while read/write size of multiple structs to file

查看:163
本文介绍了在将多个结构体的读/写大小写入文件时编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提出了两个与这个项目相关的问题,我得出这个结论。

I've already asked 2 questions kind of related to this project, and i've reached this conclusion. Writing the size of the Struct to the file , and then reading it back is the best way to do this.

我正在创建一个作业作业的程序,这个作业将会让我保持库存。我需要读/写同一类型的多个结构到一个文件。

I'm creating a program for a homework assignment that will allow me to maintain inventory. I need to read / write multiple structs of the same type to a file.

问题是...这是非常复杂,我有麻烦包装我的头围绕整个过程。我看到一堆例子,我想把它放在一起。我收到编译错误...我没有线索如何解决它们。如果你能帮助我,我会很感激...谢谢你。我现在很迷路...

The problem is... this is really complicated and i'm having trouble wrapping my head around the whole process. I've seen a bunch of examples and i'm trying to put it all together. I'm getting compile errors... and I have zero clue on how to fix them. If you could help me on this I would be so appreciative... thank you. I'm so lost right now...

** HOPFPULL最后一次编辑#3 ***********

我的代码:

// Project 5.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace System;
using namespace std;
#pragma hdrstop

int checkCommand (string line);

template<typename Template>
void readFromFile(Template&);

template<typename Template>
void writeToFile(Template&);

template<typename T>
void writeVector(ofstream &out, const vector<T> &vec);

template<typename Template>
void readVector(ifstream& in, vector<Template>& vec);

struct InventoryItem {
    string Item;
    string Description;
    int Quantity;
    int wholesaleCost;
    int retailCost;
    int dateAdded;
} ;


int main(void)
{
    cout << "Welcome to the Inventory Manager extreme! [Version 1.0]" << endl;

    vector<InventoryItem> structList;

    ofstream out("data.dat");

    writeVector( out, structList );

    while (1)
    {

    	string line = "";

    	cout << endl;
    	cout << "Commands: " << endl;
    	cout << "1: Add a new record " << endl;
    	cout << "2: Display a record " << endl;
    	cout << "3: Edit a current record " << endl;
    	cout << "4: Exit the program " << endl;
    	cout << endl;
    	cout << "Enter a command 1-4: ";

    	getline(cin , line);


    	int rValue = checkCommand(line);
    	if (rValue == 1)
    	{
    		cout << "You've entered a invalid command! Try Again." << endl;
    	} else if (rValue == 2){ 
    		cout << "Error calling command!" << endl;
    	} else if (!rValue) {
    		break;
    	}
    }


    system("pause");

    return 0;
}

int checkCommand (string line)
{
    int intReturn = atoi(line.c_str());
    int status = 3;

    switch (intReturn)
    {
    	case 1:
    		break;
    	case 2:
    		break;
    	case 3:
    		break;
    	case 4:
    		status = 0;
    		break;
    	default:
    		status = 1;
    		break;
    }
    return status;
}

template <typename Template>
void readFromFile(Template& t)
{
    ifstream in("data.dat");
    readVector(in, t); Need to figure out how to pass the vector structList via a Template
    in.close();
}

template <typename Template>
void writeToFile(Template& t)
{
    ofstream out("data.dat");
    readVector(out, t); Need to figure out how to pass the vector structList via a Template
    out.close();
}

template<typename T>
void writeVector(ofstream &out, const vector<T> &vec)
{
    out << vec.size();

    for(vector<T>::const_iterator i = vec.begin(); i != vec.end(); ++i)
    {
        out << *i; // SUPER long compile error
    }
}

template<typename T>
vector<T> readVector(ifstream &in)
{
    size_t size;
    in >> size;

    vector<T> vec;
    vec.reserve(size);

    for(int i = 0; i < size; ++i)
    {
        T tmp;
        in >> tmp;
        vec.push_back(tmp);
    }

    return vec;
}

我的编译错误:

1>.\Project 5.cpp(128) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const InventoryItem' (or there is no acceptable conversion)
1>        C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1>        with

这是我现在得到的唯一错误。我看到你的代码是更好。我的新编译器错误是SUPER长。我已经显示了错误指向的位置。最后一次你能帮我吗?

That is the only error i'm getting now. I see your code is SO Much better. My new compiler error is SUPER long. I've shown where it the error points to. Could you help me just one last time?

推荐答案

您的读写功能是有问题的。特别是,你应该这样做:

Your read and write functions are buggy. In particular, you should be doing something like this instead:

template<typename T>
void write(ofstream &out, const T &t)
{
    out << T;
}

OLD: bind1st 需要包含功能才能正常工作:

OLD: bind1st requires you do include functional for it to work:

#include <functional>

而不是处理所有这些函数等,但最好依赖迭代器:

Instead of dealing with all these functions and such, though, it'd be better to rely on iterators:

template<typename T>
void writeVector(ofstream &out, const vector<T> &vec)
{
    out << vec.size();

    for(vector<T>::const_iterator i = vec.begin(); i != vec.end(); ++i)
    {
        out << *i;
    }
}

template<typename T>
vector<T> readVector(ifstream &in)
{
    size_t size;
    in >> size;

    vector<T> vec;
    vec.reserve(size);

    for(int i = 0; i < size; ++i)
    {
        T tmp;
        in >> tmp;
        vec.push_back(tmp);
    }

    return vec;
}

您希望函数读取和写入 InventoryItem 以及,可能:

You'd want functions to read and write your InventoryItem as well, probably:

ostream &operator<<(ostream &out, const InventoryItem &i)
{
    out << i.Item << i.Description;  // FIXME Read/write strings properly.
    out << i.Quantity;
    out << i.wholesaleCost << i.retailCost;
    out << i.dateAdded;
}

istream &operator>>(istream &out, InventoryItem &i)
{
    // Keep in same order as operator<<(ostream &, const InventoryItem &)!
    in >> i.Item >> i.Description;  // FIXME Read/write strings properly.
    in >> i.Quantity;
    in >> i.wholesaleCost >> i.retailCost;
    in >> i.dateAdded;
}

这篇关于在将多个结构体的读/写大小写入文件时编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆