I am getting a bunch of errors saying "Undefined reference to ...", and cant understand why. I have read other questions with the error "Undefined reference to ...", but the answers doesent work for me. Here is part of my code:
main:
#include <iostream>
#include <armadillo>
#include <string>
#include <DombsMain.h>
using namespace std;
using namespace arma;
int main(){
cout << "Armadillo version: " << arma_version::as_string() << endl;
dombsmain::initilize("test");
return 0;
}
dombsmain.h:
#ifndef DOMBSMAIN_H_INCLUDED
#define DOMBSMAIN_H_INCLUDED
#include <string>
#include <vector>
#include "Body.h"
#include "Constraint.h"
#include <Solver.h>
namespace dombsmain {
extern void initilize(std::string fileName);
extern std::string inputfileName;
...blabla
}
#endif // DOMBSMAIN_H_INCLUDED
dombsmain.cpp:
#include <DombsMain.h>
#include <BallJoint.h>
#include <dombs.h>
#indlude blabla
using namespace std;
using namespace arma;
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
..blabla
I think the error has something to do with namespace dombsmain. DombsMain.h is included both in main and in dombsmain.cpp, but it still says that the variables and functions in the namespace in undefined. I think it might be some conflict with including DombsMain.h int both main and dombsmain.cpp. I tried deleting the #include <DombsMain.h> in main.cpp, but then I couldent call dombsmain::initilize("test");
You just forgot to define std::string inputfileName. For example you can do it in dobsmain.cpp:
namespace dombsmain{
void initilize(string infileName){
inputfileName = infileName;
}
std::string inputfileName = "";
}
Related
I'm having an issue where I am trying to use variables stored in a struct called from a header file. I am able to call to each cpp file ie., call an action from one to another however variables are proving difficult.
Client.cpp
#include <iostream>
#include "Header.h"
#include <vector>;
#include <conio.h>;
#include <string>;
using namespace std;
int main()
{
cout << "Please enter a name: " << flush;
cin >> sNam.sNames;
main2();
return 0;
}
Admin.cpp
#include <iostream>
#include "Header.h"
#include <vector>;
#include <conio.h>;
#include <string>;
using namespace std;
void main2()
{
if (sNam.sNames == sNam1.sNames1)
{
cout << "Correct Entry...\n";
}
else if (sNam.sNames != sNam1.sNames1)
{
cout << "Incorrect Entry...\n";
}
}
Header.h
#pragma once
#include "Admin.cpp"
#include "Client.cpp"
struct Names
{
string sNames;
string sNames1 = "John";
}sNam, sNam1;
don't include cpp files
also sNam and sNam1 are defined in both cpp files, you will have link time error
include Header.h in both Client.cpp and Admin.cpp
then declare what's common in Header.h
#pragma once
// #include "Admin.cpp"
// #include "Client.cpp"
struct Names
{
string sNames;
string sNames1 = "John";
};
extern Names sNam, sNam1;
void main2();
in one of cpp files
#include "Header.h"
Names sNam, sNam1;
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.
I try to compile my code, I pretty sure I made a mistake in my headers or in the compilation but I don't understand where. I know that this is a basic problem, and I read some other topic, but I really don't understand. And I watch some other code I wrote and I don't see any difference...
g++ -c main.cpp -o out
I don't understand the error, so I also try :
g++ -c readFastqFile.cpp
The error
readFastqFile.cpp:8:1: error: ‘readFastq’ does not name a type
readFastq::readFastq(){ //Constructor
And my files are :
main.cpp
#include <iostream>
#include <string>
#include "readFastqFile.hpp"
using namespace std;
int main(int argc, char *argv[]){
cout << "hello" <<endl;
//readFastq allReads;
return 0;
}
readFastqFile.hpp
#ifdef READFASTQFILE_HPP
#define READFASTQFILE_HPP
#include <iostream>
#include <string>
using namespace std;
class readFastq{
public:
readFastq(); //constructor
private:
string readName;
string sequence;
string score;
};
#endif // READFASTQFILE_HPP
readFastqFile.cpp
#include <string>
#include <iostream>
#include "readFastqFile.hpp"
using namespace std;
readFastq::readFastq(){ //Constructor
readName = "bla";
cout << readName <<endl;
}
Thanks
#ifdef READFASTQFILE_HPP should be #ifndef READFASTQFILE_HPP. The #ifdef is causing the contents of readFastqFile.hpp to be ignored, so the class definition isn't being compiled.
See also Include guards
I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.
I am interested in being able to share a defined global variable across two cpp files. Is the following possible? I am interested in this to avoid having to initialize the global shared variable multiple times. I am having trouble being able to build this code. How would you recommend to declare/define myMap in this case?
MapHeader.h
#ifndef _MAP_HEADER_
#define _MAP_HEADER_
#include <string>
#include <map>
using namespace std;
extern const map<int, string> myMap = {{100 , "one hundred"}, {200,"two hundred"}, {300,"three hundred"}};
#endif // _MAP_HEADER_
FirstFile.h
#ifndef _FIRST_FILE_
#define _FIRST_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction1();
#endif
FirstFile.cpp
#include "FirstFile.h"
void myFunction1(){
cout << "myFunction1()" << myMap[100] << endl;
}
SecondFile.h
#ifndef _SECOND_FILE_
#define _SECOND_FILE_
#include "MapHeader.h"
#include <iostream>
#include <string>
using namespace std;
void myFunction2();
#endif
SecondFile.cpp
#include "SecondFile.h"
void myFunction2(){
cout << "myFunction2()" << myMap[200] << endl;
}
Main.cpp
#include "FirstFile.h"
#include "SecondFile.h"
int main()
{
myFunction1();
myFunction2();
return 0;
}
I am getting the error message:
error: passing 'const std::map<>' as 'this' argument of 'std:map<>' .....
In MapHeader.h, change your definition to extern map myMap; and then move your definition exactly as you had it in MapHeader.h into one of the .cpp's.