This question already has answers here:
cout is not a member of std
(3 answers)
Closed 1 year ago.
I've looked at a lot of solutions for this problem but none have worked
Main:
#include "player.cpp"
#include "player.h"
#include <iostream>
#include <SDL.h>
using namespace std;
int main() {
player p;
SDL_Init(SDL_INIT_EVERYTHING);
for (;;) {
p.move()
}
}
player.h:
#pragma once
#ifndef PLAYER_H
#define PLAYER_H
class player {
private:
short x = 0;
short y = 0;
public:
void move() {
std::cout << x << '\n';
x += 1;
}
};
#endif
player.cpp:
#include "player.h"
#include <iostream>
It keeps saying cout is not a member of std, what am I doing wrong?
The player.h file does not have a #include <iostream> in it.
You have two choices:
Make every header file able to stand on its own.
Ensure that you document and meet the pre-requisites for every header file you include.
You have done neither of these things.
Related
This question already has answers here:
C++: Has Not Been Declared
(2 answers)
Closed 4 years ago.
I don't know what is going on. I included file guards in header files but still get the error.
these are my classes:
Car.h
#ifndef CAR_H
#define CAR_H
#include "Color.h"
class Car
{
public:
Car(Color a);
void printInfo();
private:
Color carColor;
};
#endif
Car.cpp
#include "Car.h"
#include <iostream>
using namespace std;
Car::Car(Color a)
: carColor(a)
{
}
void Car::printInfo() {
cout << "the car is ";
carColor.printColor();
}
Color.h
#ifndef COLOR
#define COLOR
#include "Car.h"
#include <iostream>
using namespace std;
class Color
{
public:
Color(string c);
void printColor();
private:
string colorr;
};
#endif // COLOR
Color.cpp
#include "Color.h"
Color::Color(string c)
: colorr(c)
{
}
void Color::printColor() {
cout << colorr;
}
Edit:
guys I didn't insult to anybody. imagine you wrote lots of words and stackoverflow gives you error about question and you edit but still the same and again edit and ... this happens again and again. what should I write more ?? the question is simple and short even don't need to explain it.
For starters, you should remove #include "Car.h" from Color.h.
It creates an unnecessary circular include, and the compiler hits Car(Color a) before it knows that Color is a class.
You also need to include the header <string> to output a string to cout.
Next time, maybe don't insult the people who are helping you.
This question already has answers here:
Link error when declaring public static variables in C++
(2 answers)
Closed 6 years ago.
I am new to C++ and I am trying to start a project where every time I create a new instance of the ATM class it incements accountID by 1 and displays the current account ID.
This is my code:
// Bank ATM.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "ATM.h"
int main()
{
ATM abunch[15];
for (int i = 0; i < 15; i++){
abunch[i] = ATM();
}
return 0;
}
//ATM.h
#include "stdafx.h"
#ifndef atm
#define atm
class ATM {
static int accountID;
public:
ATM();
};
int ATM::accountID = 0;
#endif
//ATM.cpp
#include "stdafx.h"
#include "ATM.h"
#include <iostream>
ATM::ATM() {
++accountID;
std::cout << accountID;
}
I get The following error message:
What am I doing wrong?
Because ATM::accountID is declared in a .h file, outside of a class, it is globally declared every time that file is included in another file. You include it twice; in main.cpp and in ATM.cpp. That's a no-no.
The declaration needs to move to ATM.cpp
//ATM.h
#include "stdafx.h"
#ifndef atm
#define atm
class ATM {
static int accountID;
public:
ATM();
};
int ATM::accountID = 0; // <--- remove this line
#endif
//ATM.cpp
#include "stdafx.h"
#include "ATM.h"
#include <iostream>
int ATM::accountID = 0; // <----put it here
ATM::ATM() {
++accountID;
std::cout << accountID;
}
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 years ago.
Please see my previous post here:
Undefined type error even with forward declaration
I moved the definitions to cpp files and I still face the issue. Any ideas why? My files look like this:
Header1.hpp
#ifndef HEADER1_HPP
#define HEADER1_HPP
namespace sample_ns
{
class sample_class{
public:
static int getNumber();
static void print();
};
}
#endif
Header2.hpp
#ifndef HEADER2_HPP
#define HEADER2_HPP
namespace sample_ns
{
class sample_class2{
public:
sample_class2();
int getNumber2();
};
}
#endif
Source1.cpp
#include "Header1.hpp"
#include "Header2.hpp"
#include "stdafx.h"
#include <iostream>
namespace sample_ns
{
int sample_class::getNumber()
{
sample_class2 obj;
return obj.getNumber2();
}
void sample_class::print()
{
std::cout << "Print utility function" << std::endl;
}
}
Source2.cpp
#include "Header2.hpp"
#include "Header1.hpp"
#include "stdafx.h"
#include <iostream>
namespace sample_ns
{
sample_class2::sample_class2()
{
sample_class::print();
}
int sample_class2::getNumber2()
{
sample_class::print();
return 5;
}
}
In my main I call it as:
std::cout << sample_ns::sample_class::getNumber() << std::endl;
I get 'sample_class2' : undeclared identifier. I tried adding class sample_class2; but that still gives me error
EDIT:
my main file:
#include "stdafx.h"
#include <iostream>
#include "Header1.hpp"
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello" << std::endl;
std::cout << sample_ns::sample_class::getNumber() << std::endl;
return 0;
}
The best practice for declaring classes and namespaces in header and cpp files is using structure likes below:
Header1.hpp
#ifndef HEADER1_HPP
#define HEADER1_HPP
#include "Header2.hpp"
#include <iostream>
namespace sample_ns
{
class sample_class{
public:
static int getNumber();
static void print();
};
}
#endif
Source1.cpp
#include "Header1.hpp"
namespace sample_ns
{
int sample_class::getNumber()
{
sample_class2 obj;
return obj.getNumber2();
}
void sample_class::print()
{
std::cout << "Print utility function" << std::endl;
}
}
So by including in header files and using ifndef you become sure that circular dependencies will not occure.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm having an undefined reference error in my main. Not sure what I'm doing wrong and have tried changing names and moving things around but keep getting the same error. I'm wondering if maybe it's my IDE but really don't know
Here's the code:
#include <iostream>
#include "f.h"
#include "g.h"
using namespace std;
int main()
{
F f;
G g;
f.f();
g.g();
return 0;
}
next file:
#ifndef F_H_INCLUDED
#define F_H_INCLUDED
class F
{
public:
void f();
};
#endif
next file:
#ifndef G_H_INCLUDED
#define G_H_INCLUDED
class G
{
public:
void g();
};
#endif
next file:
#include "f.h"
#include <iostream>
void F::f()
{
std::cout << "This was function f!" << std::endl;
}
next file:
#include "g.h"
#include <iostream>
void G::g()
{
std::cout << "This was function g!" << std::endl;
}
edit: so i changed the include from "f.h" and "g.h" to "f.cpp" and "g.cpp" and now it works... can anyone explain why?
It's looks fine, but make sure if your files names are correct, there is difference between lower and upper case.
#include "f.h"
#include "g.h"
its not the same as
#include "F.h"
#include "F.h"
I test it with VS2013, it is right, if you copy code, please check it.
I am new C++ and I have a question about separate files classes in Xcode. I did write a program trying to lean class, but I got an error. Could anyone teach me how to do that right ?
I include the program that I tried below
this is the (main CCP):
#include <iostream>
#include "hhhhhhhhhhhhhhhh.h"
using namespace std;
int main ( int argc, char ** argv )
{
bassam bo;
bo.bassamfunction();
}
this is the (.h) :
#ifndef __try_some_concspte__hhhhhhhhhhhhhhhh__
#define __try_some_concspte__hhhhhhhhhhhhhhhh__
#include <iostream>
class bassam{
public:
void bassamfunction();
};
#endif /* defined(__try_some_concspte__hhhhhhhhhhhhhhhh__) */
this is the (CCP):
#include <iostream>
#include "hhhhhhhhhhhhhhhh.h"
using namespace std;
bassam::bassamfunction()
{
cout << " heloo I am here "<< endl ;
}
Your problem is that bassam::bassamfunction() has no return type in the .cpp file. This should be changed to void bassam::bassamfunction().