Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
There are many questions on this, but none solve my problem. I am trying to use multiple files. I have no idea where the problem is, so I will include all the top lines (#include, #pragma, etc.)
This problem arose when I was creating the class for Cubes.cpp (and in Cubes.h).
main.cpp
#include <iostream>
#include "Cubes.h"
#include "Common functions.h"
#include "Global.h"
#include <SFML/Graphics.hpp>
Cubes.cpp
#include <iostream>
#include "Cubes.h"
#include "Common functions.h"
#include "Global.h"
#include <SFML\Graphics.hpp>
#include <vector>
#include <string>
// Later on in code, where I think it went wrong:
Render::Render()
{
this->rot;
this->boxCoords;
this->rlBoxCol;
this->gridSize;
this->cubeSize;
this->offset;
}
void Render::setRotation(std::vector<float> setRot)
{ // Set rotation
rot = setRot;
}
std::vector<float> Render::getRotation()
{ // Get rotation
return rot;
}
void Render::setCoordinates(std::vector<int> setBoxCoords)
{
boxCoords = setBoxCoords;
}
std::vector<int> Render::getCoordinates()
{
return boxCoords;
}
void Render::setColours(std::vector<std::string> setRlBoxCol)
{
rlBoxCol = setRlBoxCol;
}
std::vector<std::string> Render::getColours()
{
return rlBoxCol;
}
void Render::setSizeOfGrid(int setGridSize)
{
gridSize = setGridSize;
}
int Render::getSizeOfGrid()
{
return gridSize;
}
void Render::setSizeOfCubes(int setCubeSize)
{
cubeSize = setCubeSize;
}
int Render::getSizeOfCubes()
{
return cubeSize;
}
void Render::setOffset(std::vector<int> setOffset)
{
offset = setOffset;
}
std::vector<int> Render::getOffset()
{
return offset;
}
void Render::display()
{
// Long code, not going to show it
}
Cubes.h
#pragma once
#include <vector>
#include <string>
// Also later on in Cubes.h ...
class Render
{
private:
std::vector<float> rot;
std::vector<int> boxCoords;
std::vector<std::string> rlBoxCol;
int gridSize;
int cubeSize;
std::vector<int> offset;
public:
Render();
void setRotation(std::vector<float> setRot);
std::vector<float> getRotation();
void setCoordinates(std::vector<int> setBoxCoords);
std::vector<int> getCoordinates();
void setColours(std::vector<std::string> setRlBoxCol);
std::vector<std::string> getColours();
void setSizeOfGrid(int setGridSize);
int getSizeOfGrid();
void setSizeOfCubes(int setCubeSize);
int getSizeOfCubes();
void setOffset(std::vector<int> setOffset);
std::vector<int> getOffset();
void display();
};
Common functions.cpp
#include <iostream>
#include "Common functions.h"
#include <vector>
#include <string>
Common functions.h
#pragma once
#include <vector>
Global.h
#pragma once
#include <SFML\Graphics.hpp>
extern sf::RenderWindow Window(sf::VideoMode(500, 500), "Maximize window to play the game");
extern std::string status = "NULL";
With the errors:
LNK2005
"class sf::RenderWindow Window" (?Window##3VRenderWindow#sf##A) already defined in Cubes.obj
C:\Users\George\Documents\C++\Projects\Don't fall\Don't fall\main.obj 1
.
LNK2005
"class std::basic_string,class std::allocator > status" (?status##3V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A) already defined in Cubes.obj
C:\Users\George\Documents\C++\Projects\Don't fall\Don't fall\main.obj 1
.
LNK1169
one or more multiply defined symbols found
C:\Users\George\Documents\C++\Projects\Don't fall\Debug\Don't fall.exe 1
.
I have been stuck on this problem for a while, so any help would be greatly appreciated!
EDIT
I answered my own question, as no one found a solution.
Solution (solved my own problem)
In Global.h, I changed the code to:
#pragma once
#include <SFML\Graphics.hpp>
extern sf::RenderWindow Window;
extern std::string status;
and in Cubes.cpp, I changed it to:
#include <iostream>
#include "Cubes.h"
#include "Common functions.h"
#include "Global.h"
#include <SFML\Graphics.hpp>
#include <vector>
#include <string>
sf::RenderWindow Window(sf::VideoMode(500, 500), "Maximize window to play the game");
std::string status = "NULL";
Related
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.
I have the following issue thrown by the compiler:
include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope
std::vector grid;
when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):
LatticeCell.h
#ifndef LATTICECELL_H
#define LATTICECELL_H
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
/* Single cell */
using namespace std;
class LatticeCell{
private:
std::vector<double> matrix = {0,0,0,0,0,0,0,0,0};
unsigned int type; //fluid, no-slip, velocity or density
public:
//Constructor
LatticeCell(unsigned int inType){
type = inType;
}
};
#endif
FlowChannel.h
#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"
using namespace std;
class FlowChannel{
private:
std::vector<LatticeCell> grid; //ERROR LINE
unsigned int dimX = -1;
unsigned int dimY = -1;
public:
FlowChannel(unsigned int nx, unsigned int ny){
dimX = nx+2;
dimY = ny+2;
unsigned int gridSize = dimX*dimY;
grid.reserve(gridSize);
initGrid(/*TODO Params*/);
}
};
#endif
lbm.cpp
#include <string>
#include <vector>
#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"
int main(int argc, char** argv){
printsomething();
return 0;
}
Utilities.cpp
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void printsomething(){
cout << "something" << std::endl;
}
double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
return 3.0 * (uin * ny / reynolds) - 0.5;
}
Utilities.h
#ifndef UTILITIES_H
#define UTILITIES_H
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>
void printsomething();
#endif
Further my compiler flags are:
-Wall -std=c++17 -pedantic
For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?
Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem
You should check if the files are in the same directory.
I copy and paste your code in VS 2019 and it work for me,
here are the pictures
FlowChannel LatticeCell
It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.
I am new to c++ . The program doesn't compile i am using Xcode. It doesn't give me any error. It just doesn't compile which's weird. Thanks for the help!
I am using 3 files as you can see. I think. there's a problem with the implementation that i can not figure out, if you have any suggestion for a better IDE that Xcode than would be helpful.
Thanks again
Car.cpp
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
void Car:: setMaker(string m)
{
maker=m;
}
string Car:: getMaker()
{
return maker;
}
void Car:: setModel(int m)
{
model=m;
}
int Car:: getModel()
{
return model;
}
Car.h
#ifndef _CAR_H;
#define _CAR_H;
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Car
{
private:
string maker;
int model;
public:
void setMaker(string m);
string getMaker();
void setModel(int m);
int getModel();
};
#endif
main.cpp
#include <iostream>
#include "Car.cpp"
#include "Car.h"
using namespace std;
int main ()
{
Car c1;
c1.setMaker("BMW");
c1.getMaker();
return 0;
}
Relearning C/C++ after 3 years of JavaScript (I've gotten way too comfortable..)
I'm building a test file with input.
The problem is within cTool, where the first function is not letting me return a string. I thought this was totally valid if the library is included in the header file? What am I overlooking here.
cTool.cpp
string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
You need to add #include "cTool.h" to cTool.cpp, not just to cMain.cpp only. Otherwise, when compiling cTool.cpp, the compiler doesn't know what a string is since it doesn't see your #include <string> and using namespace std; statements (BTW, using namespace std; in a header file is a very bad idea).
cTool.cpp
#include "cTool.h" // <-- ADD THIS!
std::string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <iostream>
std::string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am getting an error on my private variables Player_player; and Level _level; it is telling me that it is missing the type specifier and im not sure why this is. I have made classes for both Level and Player, so shouldnt I be able to use Player and Level to specify the variable type?
thanks for the help,
mike
//GameSystem.h
#pragma once
#include "Player.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
class GameSystem
{
public:
GameSystem(string levelFileName);
void playGame();
private:
Level _level;
Player _player;
};
#
//Player.h
#pragma once
#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
class Player
{
public:
Player();
void initPlayer(int level, int health, int attack, int defense, int experience);
//Setters
void setPosition(int x, int y);
//Getters
void getPosition(int &x, int &y);
private:
//Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;
//Position
int _x;
int _y;
};
Your GameSystem.h file has the line:
#include "Player.h"
Your Player.h file has the line:
#include "GameSystem.h"
This can't be good. And besides the Player class declaration in the header file doesn't use anything from GameSystem.h, so remove the GameSystem.h from the header file (I recommend removing all the #include that don't resolve anything in Player.h header file).
Edit 1:
I changed the header files and made them use Include Guards. I don't get any errors.
Player.hpp:
#ifndef PLAYER_HPP
#define PLAYER_HPP
class Player
{
public:
Player();
void initPlayer(int level, int health, int attack, int defense, int experience);
//Setters
void setPosition(int x, int y);
//Getters
void getPosition(int &x, int &y);
private:
//Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;
//Position
int _x;
int _y;
};
#endif // PLAYER_HPP
GameSystem.hpp:
#ifndef GSYSTEM_HPP
#define GSYSTEM_HPP
#include "Player.hpp"
#include "Level.hpp"
#include <string>
using namespace std;
class GameSystem
{
public:
GameSystem(string levelFileName);
void playGame();
private:
Level _level;
Player _player;
};
#endif // GSYSTEM_HPP
I changed the filename extensions to help distinguish between C language header files (.h) and C++ language header files (.hpp).
I also simplified the header files by remove unused include files.
You have a dependency problem here, there are two solutions.
First, Player.h isn't actually dependent on GameSystem.h, so don't include GameSystem.h in Player.h.
//Player.h
#pragma once
//Remove GameSystem.h
//#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
If, for some reason, you DO need the GameSystem class declaration in the Player class, just do this:
//Player.h
#pragma once
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
//Declare the class
class GameSystem;
You will need to include the full header file in the .cpp file, but you don't actually need the full class definition to use a class in another classes definition (actually, GameSystem.h doesn't even need to include Player.h, it could just declare but not define the Player class)