Global function header and implementation - c++

how can I divide the header and implementation of a global function?
My way is:
split.h
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const string s, const string c);
split.cpp
#include "split.h"
void split(const string& s, const string& c){
...
}
main.cpp
// main.cpp : Defines the entry point for the console application.
//
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include "stdafx.h"
#include "split.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> v;
string s = "The;;woraaald;;is;;not;;enoaaaugh";
string c = " aaa ;; ccc";
split(s,c);
return 0;
}
And errors are:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...\split.h 8
Error 2 error C2146: syntax error : missing ',' before identifier 's' ...\split.h 8
How can I solve this problem? thx

In header file use std:: namespace qualifier - std::string

In the header file, you have to give the fully qualified name std::string. In the source files, you can add using namespace std; or using std::string; and then just spell it string.
Also, you've declared the function taking arguments by value, but defined it taking arguments by reference.

At least one problem is, you are missing the 'std::' namespace qualifier in split.h:
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const std::string s, const std::string c);

I think you either forgot to put using std::string; before split declaration or use std::string const& as split parameter declarations.
Also split declaration mismatch from split definition string const vs string const&

Related

passing a vector of structs into a function

In a program the following struct is defined in a header file:
\\structs.h
#include <vector>
using std::vector;
using namespace std;
struct cell
{
double x;
vector<int> nn;
};
In a separate source file I define the function:
\\functions.cpp
# define _CRT_SECURE_NO_DEPRECATE
# include <stdio.h>
# include <iostream>
# include <math.h>
# include <vector>
# include "structs.h"
using namespace std;
void initial_position(vector<cell>& cluster, int n)
{
cell tmp;
for (int i = 0; i < n; i++)
{
tmp.x = 1;
cluster.push_back(tmp);
}
}
with a header file:
//functions.h
# include <vector>
using std::vector;
void initial_position(vector<cell>& cluster, int n);
I wish to call this function in the main script:
//main.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <vector>
#include "functions.h"
#include "structs.h"
using namespace std;
int main()
{
vector <cell> cluster;
int n = 100;
initial_position(cluster,n);
return 0;
}
but get the following errors:
functions.h(4): error C2065: 'cell': undeclared identifier
functions.h(4): error C2923: 'std::vector': 'cell' is not a valid template type argument for parameter '_Ty'
functions.h(4): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type
main.cpp(14): error C2664: 'void initial_position(std::vector &)': cannot convert argument 1 from 'std::vector>' to 'std::vector &'
What is the source of the errors? it all seems to be well defined.
Put
#include "structs.h"
into functions.h and protect both structs.h and functions.h with include-guards, e.g.
#pragma once
if available.
add
#include "structs.h"
into functions.h since in functions.h compiler doesn't know what cell is.
Just swap
#include "functions.h"
#include "structs.h"
with
#include "structs.h"
#include "functions.h"
Since cell is declared in structs.h and is needed in functions.h
Or better yet, include structs.h in functions.h
You should also not place using namespace std in a header, that is bad practice. It can cause some nasty hard to find bugs, see e.g. Why is "using namespace std" considered bad practice?

Reference to "class" is ambigous

I would like to implement a hash table example.
So for this aim, I have created one header, one hash.cpp and main.cpp files.
in my hash.cpp , I tried to run a dummy hash function which takes key value and turns into an index value. however, it throws an error(reference to 'hash' is ambiguous) whenever I try to create an object according to that hash class.
this is my main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
this is my hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
this is my hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key);
};
#endif /* defined(__hashtable__hash__) */
Your hash class symbol is clashing with std::hash
A quick fix could be using a global namespace qualifier
int main(int argc, const char * argv[]) {
::hash hash_object;
but a better and recommended one would be to stop polluting your global namespace with
using namespace std;
and just using std::cout or std::endl when you need them.
You could also create your own namespace in case you're writing a library.
Besides, you have some capital letter typos here:
index = hash_object.hash("patrickkluivert");
^ I suppose you're referring to the Hash() function here
and here
int Hash(std::string key) {
^ this needs to be capital as well
int hash = 0;
in case you want to match your declaration and avoid cast/linking errors.
Your hash class is conflicting with std::hash. Stop using using namespace std; right now. If you want to make print statements shorter, try using std::cout; using std::endl;

String could not be resolved c++

I am wondering why I am getting error "string could not be resolved to type" when I have the proper inclusions?
#ifndef EVENTFILEREADER_H_
#define EVENTFILEREADER_H_
#include <string>
#include <stdlib.h>
#include <iostream>
class EventFileReader {
public:
EventFileReader(string fileName);
virtual ~EventFileReader();
};
#endif /* EVENTFILEREADER_H_ */
Your compiler is complaining about not being able to find string as a defined type.
You should add its namespace std:
EventFileReader(std::string fileName);
^^^^^
You need to specify namespace, e.g.
std::string
or put the using declaration after includes:
using std::string;

Trouble with using class types within other classes

I have absolutely no idea what's going on. I've been looking up explanations for the weirdness going on here but it seems my situation is in some ways unique. I imagined it was the order in which I include my header files in each of my files, but to no avail, I have not found a combination that seems to be the solution.
The exact error I seem to be getting is "log does not name a type" when declaring LogArray[maxLength].
One of my classes, class logmgmt:
class logmgmt
{
private:
static const int maxLength = 500;
log LogArray[maxLength];
int length;
public:
void fillLogs(int index, int iD, std::string date, double startTime, double endTime);
void displayThisLog(int index);
void setLength(int length);
};
Pre-processor directives within logmgmt.cpp:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "log.h"
#include "Logmgmt.h"
And directives within main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "employee.h"
#include "log.h"
#include "employeemgmt.h"
#include "Logmgmt.h"
Remove using namespace std.
That is polluting the global namespace with lots of symbol names that can cause these conflicts.
In your example, the function std::log becomes log. So it can no longer name a global type.

error C2146: syntax error : missing ';' before identifier

i can't get rid of these errors... i have semicolons everywhere i checked...
the code is simple:
the error takes me to the definition "string name" in article.h...
main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
#include "article.h"
int main()
{
string si;
char article[128];
vector<Article> articles;
ifstream file;
file.open("input.txt",ifstream::in);
while(!file.eof())
{
file.getline(article,128);
articles.push_back(Article(article));
}
file.close();
while(1);
return(1);
}
article.h:
#ifndef Article_H
#define Article_H
class Article
{
public:
int year;
string name;
Article(char *i_name);
};
#endif
You should add:
#include <string>
to your "article.h" header file and declare name like this:
std::string name;
It seems the string type is not defined in the artivle.h file. Try to include iostream and add using namespace std (or write std::string instead of using namespace)
You should use the std:: namespace prefix in the header, like
std::string name;