【问题标题】:File saving error [Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'文件保存错误 [错误] 无法通过 '...' 传递非平凡可复制类型 'std::string {aka class std::basic_string<char>}' 的对象
【发布时间】:2020-03-06 13:23:41
【问题描述】:

我是编码界的新手,所以我似乎无法理解这个错误。我正在创建一个产品管理程序,您可以在其中输入产品详细信息并将该数据放在一个文件中,然后通过 cmd 从文件中查看它,但是每当我使用 fprintffputs 时,此错误都会不断弹出。

我的代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

struct product{
    string Id,Name,Brand;
    int type, price, quan;
    product *next;
};
int main(){
    product *a,*b,*c,*d,*e,*f;
    a = new product;
    b = new product;
    c = new product;
    d = new product;
    e = new product;
    f = new product;
    FILE *productRecord;
    productRecord = fopen("Product Record","r");
    int choice;
    cout << "\nProduct Record\n1)Add Product\n2)Update Product Details\n3)Delete Product\n4)View Product By Brand or Type\n5)Exit\n\nEnter Choice: ";
    cin >> choice;
        if(choice == 1){
            char tempId[11],tempName[60],tempBrand[60];
            int temp_type, temp_price, temp_quan;
            productRecord = fopen("Product Record","a");
            cout << "Enter Product ID No.(Maximum of 10 Digits): ";
            cin >> tempId;
            a -> Id = tempId;
            a -> next = b;
            cout << "Enter Product Name(No Special Character): ";
            cin >> tempName;
            b -> Name = tempName;
            b -> next = c;
            cout << "Types:Canned(1),Frozen(2),Drinks(3),Produce(4),Meat/Seafood(5),Cleaning(6)\nEnter Product Type(1-6): ";
            cin >> temp_type;
            c -> type = temp_type;
            c -> next = d;
            cout << "Enter Product Brand(No spaces): ";
            cin >> tempBrand;
            d -> Brand = tempBrand;
            d -> next = e;
            cout << "Enter Product Price: ";
            cin >> temp_price;
            e -> price = temp_price;
            e -> next = f;
            cout << "Enter Product Quantity: ";     
            cin >> temp_quan;
            f -> quan = temp_quan;
            f -> next = NULL;

            fprintf(productRecord, "%s", a -> Id); //this is the error





            fclose(productRecord);
        }
        else if(choice == 2){
        }
        else if(choice == 3){
        }
        else if(choice == 4){
        }
        else if(choice == 5){
        }
    return 0;
}

【问题讨论】:

  • 你为什么在c++中使用FILE和相关指令?
  • 请复制粘贴整个错误信息。最有可能的是,它准确地说明了问题所在。
  • 请获取a couple of good C++ books并正确学习C++。简而言之,不要尝试将 C++ 对象与旧的继承 C 函数一起使用(这是你的问题...fprintf 不了解 C++ std::string 对象)。
  • 指针太多,C 太多。不要使用你正在学习的任何东西来学习。
  • 哦,好的,非常感谢您提供的所有帮助。我将尝试正确学习 C++。我只是在听我教授的课程/讲座,我认为他非常粗略地混合了 C 和 C++。无论如何,谢谢你的帮助。

标签: c++


【解决方案1】:

错误信息很清楚。您不能将stringfprintf 一起使用。

fprintf(productRecord, "%s", a -> Id);

需要改成

fprintf(productRecord, "%s", a->Id.c_str());

或者,更好的是,不要混合使用 C 和 C++,而是使用 ofstream

【讨论】:

    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多