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;
}
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 am currently testing out creating a simple class which sets a number to a private variable, following the last tutorial for deconstructors in this link:
https://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm
However, I have been running into this particular issue where it says my variable is corrupted.
Run-Time Check Failure #2 - Stack around the variable 'l1' was corrupted.
This error happens only at the end Training.cpp, when it reaches the last curly bracket.
Here I defined the Line.cpp class alongside the headers.
//Line.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Line {
public:
void setLength(double len);
double getLength(void);
Line();
private:
double length;
};
Line::Line(void){
cout << "Object is being created." << endl;
}
void Line::setLength(double len) {
length = len;
}
double Line::getLength(void) {
return length;
}
// Line.h
#pragma once
#ifndef LINE_H
#define LINE_H
class Line
{
public:
Line();
void setLength(double len);
double getLength(void);
};
#endif LINE_H
And Training.cpp calls main function which calls the Line class
// Training.cpp
#include "stdafx.h"
#include <string>
#include <iostream>
#include "Line.h"
using namespace std;
int main()
{
Line l1;
l1.setLength(10.0);
cout << "Length of line: " << l1.getLength() << endl;
return 0;
}
The only difference between the tutorial and my version is that I have extracted the class Line and put it in a class file called Line, which is called by Tranining which has the main function in it.
I have extensively searched for other versions of this error but most of it mentions that it seems to be some form overstepping array boundaries. However I have not assigned any form of char array and am entirely clueless as to why the error is happening. Could any of you help me out on this?
Thanks!
You defined twice Line class.
Once in Line.h and another time in Line.cpp.
They also differs (in Line.h it hasn't the member double length; )
Your Line files shoud be like this:
//Line.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
Line::Line(void){
cout << "Object is being created." << endl;
}
void Line::setLength(double len) {
length = len;
}
double Line::getLength(void) {
return length;
}
// Line.h
#pragma once
#ifndef LINE_H
#define LINE_H
class Line
{
public:
Line();
void setLength(double len);
double getLength(void);
private:
double length;
};
#endif LINE_H
Have followed Federico's answer and the error disappeared. Thanks for the patience!
Just had to add the header file into the Line.cpp and it worked.
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 am currently trying to create a simple implementation of a Module class and a Student class in c++. These classes will incorporate specific modules and the individual students enrolled in them. However, I can't seem to get round this particular error message whenever I try to make a Module or Student object.
It will probably be something simple I missed but I have uploaded my header and source files below. Please help, this is driving me crazy. Thanks in advance.
student.h:
#include "stdafx.h"
#include <string>
class Student {
public:
Student(std::string, std::string, int);
std::string getName() const { return name; }
std::string getDegree() const { return degree; }
int getLevel() const { return level; }
private:
std::string name;
std::string degree;
int level;
};
module.cpp:
// student.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "module.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
void Module::enrol_student(Student studentY) {
students.push_back(studentY);
}
void Module::attendance_register() {
string nameX;
cout << "Attendance Register:\n" << endl;
for (int i = 0; i < students.size(); i++) {
Student studentX = students.at(i);
nameX = studentX.getName();
cout << nameX << endl;
}
}
module.h:
#include "stdafx.h"
#include "student.h"
#include <string>
#include <vector>
class Module {
public:
Module(std::string, std::string, std::vector<Student>);
std::string getModCode() { return modCode; }
std::string getModTitle() { return modTitle; }
void attendance_register();
void enrol_student(Student);
private:
std::string modCode;
std::string modTitle;
std::vector<Student> students;
};
testCode.cpp
// testCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include "module.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main() {
//Initial Test Data
Student student1("Arthur Smith", "Computer Science", 1);
return 0;
}
You need to define the constructors you declared in your classes. In student.cpp you need something like this:
Student::Student(std::string name, std::string degree, int level) : name(name), degree(degree), level(level)
{
}
This will initialise the members with the values provided.
And similar for Module.
Recently I've been learning how to create methods within classes so that I only have to write a method once and for each of that class I instantiate I can call the one method and it will work only on the variables of the object that called it, I know how to do this when only using main.cpp and no headers however I am confused on how I should be writing this when I use a class header and cpp.
I have a sample of code similar to what I want to achieve:
#include <iostream>
using namespace::std;
class Object
{
public:
int stuff;
void manageStuff();
Object();
};
void Object::manageStuff()
{
stuff++;
}
Object::Object() : stuff(0) {}
Object object1, object2;
int main() {
for (int i = 0; i < 10; i++)
{
object1.manageStuff();
object2.manageStuff();
cout << object1.stuff << "\n";
cout << object2.stuff << "\n";
}
}
This works fine and allows me to have two instances of Object and a method that works independently for each instance, this is my current project:
main.cpp:
#include <iostream>
#include "Test.h"
using namespace std;
int main()
{
Test test;
for (int i = 0; i < 10; i++)
{
test.count(); // Here's my error "undefined reference to Test::count"
}
return 0;
}
Test.cpp
#include <iostream>
#include "Test.h"
using namespace std;
Test::Test()
{
//ctor
}
Test::~Test()
{
//dtor
}
Test.h
#include <iostream>
#ifndef TEST_H
#define TEST_H
class Test
{
public:
Test();
virtual ~Test();
void count();
int counter();
};
#endif // TEST_H
and finally TestFunctions.h
#include <iostream>
#include "Test.h"
#ifndef TESTFUNCTIONS_H_INCLUDED
#define TESTFUNCTIONS_H_INCLUDED
void Test::count()
{
Test::counter++;
std::cout << Test::counter;
}
#endif // TESTFUNCTIONS_H_INCLUDED
I'm sure that there will be something that's very obviously wrong to a more seasoned programmer and I'm about to look a bit thick but any help would be greatly appreciated
Thanks!
I would suggest getting rid of TestFunctions.h, and adding the implementation of Test::count() to Test.cpp. Currently, the TestFunctions.h header is not included anywhere, so you have no access to the definition from main.
You defined (i.e. implemented) Test::count() in a header file (TestFunctions.h), but you never included it anywhere so the code there is not compiled.
You should change it to be in a .cpp file, compile it and link it with the other source files. There's no reason why not to place it in Test.cpp.
Rename TestFunctions.h into TestFunctions.cpp, make it compiled same way as main.cpp and linked.
Alternatively, include TestFunctions.h somewhere, e.g. main.cpp