I am getting an error when I declare a class:
#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;
int main(void)
{
test links;
string content="this is an string";
links.getcont(content);
}
test.h
#ifndef TEST_H_
#define TEST_H_
#include<string>
using namespace std;
class test {
public:
string getcont(string content);
};
#endif /* TEST_H_ */
test.cpp
#include "test.h"
#include <iostream>
using namespace std;
string getcont(string content)
{
cout << content;
return content;
}
When I run this I get this error:
undefined reference to test::getcont(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
Well , in your test.cpp file replace the getcont function for this
string test::getcont(string content){ //code here; }
The problem is that you are not saying that getcont is a member function of the test class.
Also, consider making it a const function and passing a const string reference
string
test::getcont( const string& content) const
{
return content;
}
Related
I'm starting with C++ and I'm trying to create a class program, however, when I try to run it appears the error: C++: Undefined reference to 'NameSpace::Class::Constructor. The code is shown below:
Header for ClassA:
//ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include <string>
namespace ClassANameSpace
{
class ClassA
{
private:
std::string attribute1;
double attribute2;
public:
ClassA();
ClassA(std::string pAttribute1, double pAttribute2);
void setClassA(std::string pAttribute1, double pAttribute2);
std::string getAttribute1() { return attribute1; }
double getAttribute2() { return attribute2; }
};
}
#endif
Class A:
//ClassA.cpp
#include "ClassA.h"
using namespace ClassANameSpace;
ClassA::ClassA()
{
}
ClassA::ClassA(std::string pAttribute1, double pAttribute2)
{
setClassA(pAttribute1, pAttribute2);
}
void ClassA::setClassA(std::string pAttribute1, double pAttribute2)
{
attribute1 = pAttribute1;
attribute2 = pAttribute2;
}
Main:
// main.cpp
#include <iostream>
#include "ClassA.h"
using namespace std;
using namespace ClassANameSpace;
int main(int argc, char const *argv[])
{
ClassA classA ("string", 1.0);
return 0;
}
When I try to run it appears: undefined reference to `ClassANameSpace::ClassA::ClassA(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, double)'
Someone can guide me to undestand what is going on?
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;
}
My code gives:
undefined reference to `NgramTree::generateTree(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
collect2.exe: error: ld returned 1 exit status
error and I dont understand why.
Here is a sample of my code.
NgramTree.cpp
#include "NgramTree.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void generateTree(string fileName, int n)
{
string line;
string ngram;
bool isWord = 1;
bool firstTime = 1;
ifstream myFile(fileName);
if (!myFile.is_open())
return;
...
NgramTree.h
#include <string>
class NgramTree {
public :
NgramTree (){ };
~NgramTree(){ };
void addNgram (std::string ngram );
int getTotalNgramCount ();
void printNgramFrequencies ();
bool isComplete ();
bool isFull ();
void generateTree(std::string fileName, int n);
};
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "NgramTree.h"
using namespace std;
int main(){
NgramTree tree;
tree.generateTree("example.txt", 3);
return 0;
}
You need to add the class name in the cpp file. Write this, so that the compiler knows which class the method belongs to:
void NgramTree::generateTree(string fileName, int n) {
...
I have this class (hashMap.h):
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
using std::cout;
using std::vector;
using std::endl;
using std::string;
class hashMap
{
public:
explicit hashMap(int hashEntrySize = 101) : hashVector(nextPrime(2 * hashEntrySize)), currentSize{ 0 }
{}
bool containsKey(const string & searchKey);
bool containsVector(const vector<string> searchVector);
void insert(const string & keyTarget, const vector<string> & insertVector);
void insertAfterReHash(const string & keyTarget, const vector<string> & insertVector);
int getCurrentSize() const;
void assignKey(string & newKey);
private:
enum EntryType { ACTIVE, EMPTY, DELETED };
struct hashEntry
{
vector<string> vectorValue;
EntryType status;
int keyID;
string key;
hashEntry(EntryType s = EMPTY)
:status(s), keyID{ -1 } {}
};
size_t hashFunction(const string & key);
bool isActive(int currentPos) const;
int findPos(const string & keyTarget);
void reHash();
vector<hashEntry> hashVector;
int currentSize;
};
And a function header file (functions.h):
#pragma once
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
{
hashMap hm(500);
//do stuff with object
return hm;
}
And the main file:
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
#include "hashMap.h"
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
int main()
{
vector<string> words;
string line;
ifstream dictionaryFile;
dictionaryFile.open("largedictionary.txt");
words = readinWords(dictionaryFile);
dictionaryFile.close();
hashMap hm = computeAdjacentWords(words);
return 0;
}
I created the hashMap class and I want to be able to return a hashMap object, but this is giving me an error of "Error C4430 missing type specifier - int assumed." What am I doing wrong?
I put the code in files and nicely asked the compiler to do its job. This is the first warning from the list:
$ cc main.cpp -c
In file included from main.cpp:5:
In file included from ./hashMap.h:6:
./functions.h:16:1: error: unknown type name 'hashMap'
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
^
The compiler doesn't know what hashMap is. When it reaches the line with the error, the hashMap symbol was not yet declared or defined.
You shouldn't define functions in header files.
Rename functions.h to functions.cpp, add #include "functions.h" at the end of the list of includes.
Create a new file functions.h that contains only the declarations of the functions (the function header) and the types they use:
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
#pragma once
//#include <iostream>
#include <vector>
#include <string>
//#include <fstream>
#include "hashMap.h"
using std::string;
using std::vector;
// Do you really need all these types here?
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words);
#endif // __FUNCTIONS_H__
You are including functions.h from hashmap.h BEFORE the hashMap class is defined. As such, when the compiler reads functions.h, the hashMap class is not defined.
I'm new to c++ and I usually can pick through errors and figure out what's wrong but I'm stumped.
I'm getting an error saying "line|10|error: 'string' in class 'mine' does not name a type"
Here is mine.h:
#ifndef MINE_H
#define MINE_H
#include <iostream>
#include <string>
using namespace std;
class mine
{
public:
mine();
string getName();
};
#endif // MINE_H
Here is mine.cpp:
#include "mine.h"
#include <iostream>
#include <string>
using namespace std;
mine::mine()
{
//ctor
}
mine::string getName()
{
}
mine::string getName()
{
}
Should have been
string mine::getName()
{
}
It should be:
string mine::getName()
{
}