简体   繁体   中英

'cout' is not a member of 'std' (Progect File c++)

I created a new project in c++ but i keep getting the same error

Main.cpp

#include <iostream>
#include <string.h>


#include "Computer.cpp"
#include "Computer.h"

using namespace std;

int main() 
{

cout << "Hello World!" << endl;
return 0;
}

Computer.h

#ifndef COMPUTER_H_INCLUDED
#define COMPUTER_H_INCLUDED
#include <string>
class Computer
{
public:
    std::string marca;
    float prezzo;
    bool acceso;

    Computer();

    void Accenditi();
    void Spegniti();
    void ImpostaMarca(std::string m);
    void ImpostaPrezzo(float p);

};

#endif

Computer.cpp

#include "Computer.h"

Computer::Computer()
{

}
void Computer::Accenditi()
{
if(!acceso)
{
    acceso = true;
}
else
{
    std::cout << "Sono già acceso";
}
}
void Computer::Spegniti()
{
if(acceso)
{
    acceso = false;
}
else
{
    std::cout << "Sono già spento";
}
}
void Computer::ImpostaMarca(std::string m)
{
marca = m;
}
void Computer::ImpostaPrezzo(float p)
{
prezzo = p;
}

The Problem

i don't understand what's wrong with Computer.cpp, i keep getting "cout is not a member of std". I tryed to add "using namespace std" and i also tryed to add the library #include but i get a new file called "makefile.win". How can i fix this error?

You need to include iostream header in your Computer.cpp file as such:

include <iostream>

and to make your life easier, you can also add:

using std::cout;
using std::endl;

right at the bottom of your include, that way you don't have to keep adding "std::cout" everytime, you can just use "cout"

Also want to add:

You can remove the include computer.cpp from your main.cpp and just leave the header. The C++ linker will automatically link your computer.h and computer.cpp together since .cpp includes the header, and your main includes the computer.h

Add # include <iostream> at the files that you use std::cout and std::cin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM