Undefined reference to files [duplicate] - c++

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.

Related

Undefined reference to function of class [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I'm dealing with oop and cannot understand what is wrong. When i try to compile code i get next messsage: undefined reference to `N::my_class::do_something()'. Code is from microsoft: https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-160
my_class.h file:
namespace N
{
class my_class
{
public:
void do_something();
};
}
my_class.cpp file:
#include "my_class.h" // header in local directory
#include <iostream> // header in standard library
using namespace N;
using namespace std;
void my_class::do_something()
{
cout << "Doing something!" << endl;
}
my_program.cpp file:
#include "my_class.h"
using namespace N;
int main()
{
my_class mc;
mc.do_something();
return 0;
}
Your code works for me on g++ 8.4.0.
using namespace N; might not enough to declare member function in N for your compiler.
Try to specify the namespace either inline or using a namespace scope in your cpp file.
EDIT: most likely problem. my_class.cpp is not compiled or linked. Check your compile parameters.

'cout': is not a member of 'std' [duplicate]

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.

Undefined Reference when compiling C++

My code is similar to this one, but the problem is exactly the same: I'm getting an "undefined reference to `Test1::v" in Test1.cpp and in Test2.cpp when compilling the program in VSCode. What am I doing wrong? I'm a bit new on c++ so I just downloaded an extension that made me a project in c++ automatically. When I run the program using Ctrl + Shift + B it gives me this error, but when I do it with the Code Runner extension it doesn't detect the .cpp files.
// Test1.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST1_H
#define TEST1_H
class Test1{
public:
Test1();
static vector<Test1> v;
int a;
};
#endif
//Test1.cpp
#include "Test1.h"
Test1::Test1(){
a = 2;
v.push_back(*this);
}
//Test2.h
#include <iostream>
#include <vector>
using namespace std;
#ifndef TEST2_H
#define TEST2_H
class Test2{
public:
Test2();
double var;
};
#endif
//Test2.cpp
#include "Test2.h"
#include "Test1.h"
Test2::Test2(){
var = 5;
Test1::v[0].a += var;
}
//main.cpp
#include <iostream>
#include "Test1.h"
#include "Test2.h"
using namespace std;
int main(int argc, char *argv[])
{
cout << "Hello world!" << endl;
}
You have declared the static vector in the header file, but you need to define it in a cpp file. Add:
vector<Test1> Test1::v;
to your test1.cpp file. You can learn more about definition vs declaration here.
Also make sure you read this: Why is "using namespace std;" considered bad practice?
You could prepend the class name to call the variable directly since it's static. So, you could do something like:
Test1::Test1(){
// v.push__back(*this); // previous
Test1::v.push_back(*this); // now
}
in Test1.cpp. You'll then get a reference tooltip on your VS Code:
static std::vector<Test1> Test1::v
Which proves it's done.

expected ')' before object. class does not name a type [duplicate]

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.

Issue with circular dependency even after separating definitions [duplicate]

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.