Good day! It does not reach how to properly "write" the code written by me. Perhaps the error is related to fstream?

main.cpp file

 #include "stdafx.h" #include "Steck.h" int main() { setlocale(LC_ALL, "Russian"); file.open("Text.txt"); if (!file.is_open()) { cout << "Файл не найден!"; _getch(); return 1; } Stack1 *st1; Stek2 *st2; bool done = false; int temp; while (!done) { switch (menu()) { case 1: st1->file_input_push_ch(st1); st2->file_input_push_nech(st2); break; case 2: cout << endl << "Номер состава:"; cin >> temp; if (temp % 2 == 0) st1->push_ch(st1, temp); else st2->push_nech(st2,temp); cout << endl; break; case 3: st1->print_ch(st1); st2->print_nech(st2); break; case 4: cout << " Конец работы. " << endl; done = true; break; default: cout << endl << " Внимание!!! Вводите только цифры от 1-го до 4-ёх!" << endl; break; } } _getch(); return 0; } 

Steck.h file

 #pragma once #ifndef FILENAME_H #define FILENAME_H #include <fstream> #include <iostream> #include <conio.h> using std::ifstream; using std::cout; using std::cin; using std::endl; ifstream file; int menu(); class Stack1 { public: Stack1() :ch(0), next(0) { cout << endl << "Constructor1 working..." << endl; } ~Stack1(); void push_ch(Stack1*&, int); void file_input_push_ch(Stack1*&); void print_ch(Stack1*); private: int ch; Stack1 *next; }; class Stek2 { public: Stek2() :nech(0), next(0) { cout << endl << "Constructor2 working..." << endl; } void push_nech(Stek2*&, int); void file_input_push_nech(Stek2*&); void print_nech(Stek2*); ~Stek2(); private: int nech; Stek2 *next; }; #endif // !FILENAME_H 

And the Stack.cpp file

 #include "stdafx.h" #include "Steck.h" int menu() { char buf[10]; int option; do { cout << endl << "1 - Ввести данные с файла." << endl; cout << endl << "2 - Ввести данные вручную." << endl; cout << endl << "3 - Вывести состояние сортировочного узла" << endl; cout << endl << "4 - Выход из программы." << endl; cin >> buf; option = atoi(buf); } while (!option); cin.get(); return option; } void Stack1::push_ch(Stack1 *&next, int temp) { Stack1 *pv = new Stack1; if (temp % 2 == 0) { pv->ch = temp; pv->next = next; next = pv; } else delete pv; } void Stack1::file_input_push_ch(Stack1 *&next) { int temp; while (!file.eof()) { file >> temp; push_ch(next, temp); } } void Stack1::print_ch(Stack1 *b) { Stack1 *print = b; if (print == NULL) cout << endl << " Чётных нет. " << endl; else { cout << endl << "Чётные номера: " << endl; while (print) { cout << print->ch << " "; print = print->next; } cout << endl; } } Stack1::~Stack1() { cout << endl << "Destructor1 working..." << endl; while (next) { Stack1 *p; p = next; next = next->next; delete p; } } void Stek2::push_nech(Stek2 *&next, int temp) { Stek2 *pv = new Stek2; if (temp % 2 != 0) { pv->nech = temp; pv->next = next; next = pv; } else delete pv; } void Stek2::file_input_push_nech(Stek2 *&next) { int temp; while (!file.eof()) { file >> temp; push_nech(next, temp); } } void Stek2::print_nech(Stek2 *b) { Stek2 *print; print = b; if (print == NULL) cout << endl << " Нечетных нет. " << endl; else { cout << endl << "Нечётные номера: " << endl; while (print) { cout << print->nech << " "; print = print->next; } cout << endl; } } Stek2::~Stek2() { cout << endl << "Destructor2 working..." << endl; while (next) { Stek2 *p; p = next; next = next->next; delete p; } } 

Trying to "build" produces the following:

Error LNK2005 "class std :: basic_ifstream std :: char_traits> file" (? File @@ 3V? $ Basic_ifstream @ DU? $ Char_traits @ D @ std @@@ std @@ A) is already defined in Stack.obj \ Users \ Semen \ Documents \ Visual Studio 2015 \ Projects \ Classes_custom_1 \ Classes_custom_1 \ Classes_custom_1.obj 1

Error LNK1169 detected a multiple character - one or more Options_Class_1 C: \ Users \ Semen \ Documents \ Visual Studio
2015 \ Projects \ Options_1_1 \ Debug \ Options_1_1.exe 1

Maybe someone had experience? Unsubscribe.

    1 answer 1

    ifstream file; in the .h file. The rule of one definition is violated.

    I don’t like the idea of ​​making the file global variable at all - but at least in the .h file, declare it as

     extern ifstream file; 

    well, in some .cpp-file already define

     ifstream file; 

    But! honestly, reconsider the decision so that there are no global variables.

    • Did as you said. Earned! - Semyon Shelukhin
    • Yes, I found another mistake! In the main objects, Stack1 * st1 and Stek2 * st2 must be assigned NULL, otherwise the view function will not work. - Semyon Shelukhin