Home c++ Multiple character detected - one or more

Multiple character detected – one or more

Author

Date

Category

I have the code. Class, implementation and main function. When everything is in one cpp file, everything works. I tried to separate the implementation of the class from the interface – I got 2 errors.
What could be the problem?

machine.h:

#pragma once
#ifndef MACHINE_H
#define MACHINE_H
class Machine {
public:
    char name[50]; // machine name
    float kol; // number of hours worked
    float kolH; // number of manufactured parts per hour
    void TEST(void);
    void TASK2(const Machine* arr, size_t n);
    void INIT(void);
    void SHOW(void);
    Machine();
    Machine(const char* na, float ko, float koH);
    Machine(const Machine& obj);
    ~Machine();
    };
#endif

machine.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include "machine.h"
using namespace std;
float kool = 0;
Machine::Machine() {
    cout << endl << "--------------------------------------------------" << endl;
    cout << "This object was created in the default constructor.\n";
    strcpy(name, "Machine Second");
    kol = 3;
    kolH = 8;
}
Machine::Machine(const char* na, float ko, float koH) {
    cout << endl << "--------------------------------------------------" << endl;
    cout << "This object was created in the constructor with parameters.\n";
    strcpy(name, na);
    kol = ko;
    kolH = koH;
}
Machine::Machine(const Machine& obj) {
    cout << endl << "--------------------------------------------------" << endl;
    cout << "This object was created in the copy constructor.\n";
    strcpy(name, obj.name);
    kol = obj.kol;
    kolH = obj.kolH;
}
void Machine::TEST(void) {
    kool += kol;
}
Machine::~Machine() {
    cout << endl << "--------------------------------------------------" << endl;
    cout << "Deleting an object with a destructor.\n";
}
void Machine::SHOW(void) {
    cout << endl << "--------------------------------------------------" << endl;
    cout << "Machine Information:\n";
    cout << "\nmachine name > " << name;
    cout << "\nnumber of hours worked > " << kol;
    cout << "\nnumber of manufactured parts per hour > " << kolH;
}
void Machine::INIT(void) {
    cout << endl << "--------------------------------------------------" << endl;
    cout << "Enter machine data:\n";
    cout << "\nName> "; cin >> name;
    cout << "\nnumber of hours worked > "; cin >> kol;
    cout << "\nnumber of manufactured parts per hour > "; cin >> kolH;
}
void Machine::TASK2(const Machine* arr, size_t n) {
    size_t i_min = 0;
    for (size_t i = 1; i < n; ++i)
        if (arr[i_min].kol > arr[i].kol)
            i_min = i;
    std::cout << arr[i_min].name << std::endl;
}

Source.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include "machine.h"
using namespace std;
float kool = 0;
int main() {
    setlocale(0, "");
    Machine M1("Machine_Big", 32, 68);
    M1.SHOW();
    Machine M2;
    ofstream fout;
    fout.open("1.txt");
    fout << M1.name << " " << M1.kol << " " << M1.kolH;
    fout.close();
    ifstream fin;
    fin.open("1.txt");
    fin >> M2.name >> M2.kol >> M2.kolH;
    fin.close();
    cout << "\n  Machine 2:\n";
    M2.SHOW();
    fin.open("1.txt");
    fin.seekg(0);
    cout << "\nStreaming content output1.txt\n";
    char ch;
    while (fin.get(ch))
        cout << ch;
    cout << "\n\nConclusion finished\n\n";
    fin.close();
    M1.TEST();
    M2.TEST();
    cout << "Number of hours worked(general): " << kool;
    cout << endl << "--------------------------------------------------" << endl;
    cout << "Populating an array of instancesma";
    Machine ma[2];
    for (int i = 0; i < 2; i++) {
        cout << "\nEntering information about" << i + 1 << " an object";
        ma[i].INIT();
    }
    for (int i = 0; i < 2; i++) {
        cout << "\nDisplaying information about" << i + 1 << " an object";
        ma[i].SHOW();
    }
    cout << "\n\nmachine name,    number of hours worked -->> ";
    ma -> TASK2(ma, 2);
    return 0;
}

Errors:

1)
ErrorLNK2005 "float kool" (?kool@@3MA) already defined inmachine.obj
2)
OgoofLNK1169 multiple character detected- one or more PR6

Answer 1, authority 100%

float kool = 0;there can be only one in the whole program. It needs to be left in only one .cpp file (meaning – in machine.cpp).

To use it from other files, you need to use extern float kool;. In order not to write this in every file, it is better to put it in the header (machine.h).


Or, if you have a fairly new compiler, instead of all this you can write in the header (machine.h) inline float kool = 0;(and remove float kool = 0;from all files).

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions