When I call readTransition() function in readDPDA() function declaration I get linker error: undefined reference.
How can I use a function defined by me in another function's declaration ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readTransitionRules(char * temp);
void readDPDA(char * filename);
int main()
{
// irrelevant
}
void readTransitionRules(char * temp)
{
char *tempToken;
tempToken=strtok(temp," ,:");
int i;
for(i=0; i<5; i++)
{
printf("%s",tempToken);
strtok(NULL," ,:");
}
}
void readDPDA(char * filename)
{
/*This function tries to open DPDA text file to read
states,alphabet,stack symbols and transition rules
of the DPDA that we will use as Word Checker. */
extern void readTransitionRules(char * temp);
char * temp;
FILE * readerForDPDA;
readerForDPDA = fopen(filename,"r");
if(readerForDPDA!=NULL)
{
fgets(temp,30,readerForDPDA);
if(temp[0]=='T')
{
readTransitionRule(temp);
}
}
else
{
}
}
In ReadDPDA you refer to a readTransitionRule and not a readTransitionRules as you have defined. You are missing the letter s.
You are calling readTransitionRule, but your function is named readTransitionRules.
You probably had a warning about implicit function declaration. Don't ignore warnings.
Related
I'm trying to make a thread every time a call a function from a class, but i can't pass the function correctly:
file.h
#include <thread>
class Class
{
public:
Class(int a);
void ThreadBase(void (*func));
int CreateThread(void (*func));
};
file.cpp
#include <thread>
Class::Class(int a)
{
/**
* ...
*/
}
void Class:ThreadBase(void (*func))
{
while(1)
{
/**
* ...
*/
}
}
int Class:CreateThread(void (*func))
{
std::thread th(Class::ThreadBase, func);
}
Error:
error: reference to non-static member function must be called
CreateThread should call std::thread with a function and arguments for the function.
The problem is here:
std::thread th(Class::ThreadBase, func);
Class::ThreadBase is not a static function; so it can't be called directly. In this case; CreateThread should call the member function "ThreadBase" of 'this.'
I coded a strcat function. But my function doesn't run in this way -----> char * mystrcat(char *s,char *t). I want to return a pointer. Can you help me?
#include <stdio.h>
void mystrcat(char *s,char *t)
{
while(*s!='\0')
s++;
s--;
while((*(s+1)=*t)!='\0')
{ s++;
t++;
}
}
int main()
{
char str[30], str1[30];
gets(str);
gets(str1);
mystrcat(str, str1);
printf("%s\n",str);
return 0;
}
Your function has no return value. If you want to return a pointer from it then just return it. And also void is incorrect for that
When you write void mystrcat(char *s,char *t) you are saying "I will not have a return value" by using void. If you want to return a pointer, this must not be void.
To return a pointer to your string, use char**.
Your string, a series of characters, is represented as a char*.
Here's an example using your code.
#include <stdio.h>
char** mystrcat(char *s,char *t)
{
char *sOrig = s;
while(*s!='\0'){
s++;
}
s--;
while( ( *(s+1) = *t) != '\0')
{
s++;
t++;
}
return &sOrig;
}
int main()
{
char str[30], str1[30];
gets(str);
gets(str1);
char** concatValuePointer = mystrcat(str, str1);
printf("Pointer is %p\n",concatValuePointer);
return 0;
}
I am trying to store a pointer to a member function in a structure which will be used to call the function later in my program.
Something like this:
// abc.h
namespace XYZ {
typedef void func(const uint8_t *buf, int len);
struct holder
{
// other members
func * storePtr;
}
} // end of namespace
the other file as:
// pqr.h
#include abc.h
namespace XYZ {
class pqr {
// data members and other functions
void func1(const uint8_t *buffer, int length);
void func2(func *section);
void func3();
}
} // end of namespace
Now my cpp file needs to store instance of this func1 in my structure member storePtr
// app.cpp
#include pqr.h
void pqr::funct3()
{
// Do something
func2(func1);
}
void pqr::func2(func * section)
{
holder h;
h.storePtr = section;
}
But I am getting compilation error at line "func2(func1);" as
"error C3867: 'pqr::func1': function call missing argument list; use '&pqr::func1' to create a pointer to member"
I have used &pqr:: to define the scope but it also doesn't solve my problem and I am not able to understand what to do.
Pointers to member function are not the same thing as pointers to normal functions - have a look at the explanation and example here: http://msdn.microsoft.com/en-us/library/k8336763.aspx
Does anyone know why this code is not compilable with VC++ 2010
class C
{
public:
void M(string t) {}
void M(function<string()> func) {}
};
void TestMethod(function<void()> func) {}
void TestMethod2()
{
TestMethod([] () {
C c;
c.M([] () -> string { // compiler error C2668 ('function' : ambiguous call to overloaded function)
return ("txt");
});
});
}
Update:
Full code example:
#include <functional>
#include <memory>
using namespace std;
class C
{
public:
void M(string t) {}
void M(function<string()> func) {}
};
void TestMethod(function<void()> func) {}
int _tmain(int argc, _TCHAR* argv[])
{
TestMethod([] () {
C c;
c.M([] () -> string { // compiler erorr C2668 ('function' : ambiguous call to overloaded function M)
return ("txt");
});
});
return 0;
}
This is a bug of the VC++ Compiler.
https://connect.microsoft.com/VisualStudio/feedback/details/687935/ambiguous-call-to-overloaded-function-in-c-when-using-lambdas
You did not post the error message, so by looking into my crystal ball I can only conclude that you suffer those problems:
Missing #includes
You need at the top
#include <string>
#include <functional>
Missing name qualifications
You either need to add
using namespace std;
or
using std::string; using std::function;
or
std::function ...
std::string ...
Missing function main()
int main() {}
Works with g++
foo#bar: $ cat nested-lambda.cc
#include <string>
#include <functional>
class C
{
public:
void M(std::string t) {}
void M(std::function<std::string()> func) {}
};
void TestMethod(std::function<void()> func) {}
void TestMethod2()
{
TestMethod([] () {
C c;
c.M([] () -> std::string { // compiler error C2668
return ("txt");
});
});
}
int main() {
}
foo#bar: $ g++ -std=c++0x nested-lambda.cc
Works fine.
I found the following code in the book "Accelerated C++" (Chapter 6.1.1), but I can't compile it. The problem is with the find_if lines. I have the necessary includes (vector, string, algorithm, cctype). Any idea?
Thanks, Jabba
bool space(char c) {
return isspace(c);
}
bool not_space(char c) {
return !isspace(c);
}
vector<string> split_v3(const string& str)
{
typedef string::const_iterator iter;
vector<string> ret;
iter i, j;
i = str.begin();
while (i != str.end())
{
// ignore leading blanks
i = find_if(i, str.end(), not_space);
// find end of next word
j = find_if(i, str.end(), space);
// copy the characters in [i, j)
if (i != str.end()) {
ret.push_back(string(i, j));
}
i = j;
}
return ret;
}
Writing this in a more STL-like manner,
#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
template<class P, class T>
void split(const string &str, P pred, T output) {
for (string::const_iterator i, j = str.begin(), str_end = str.end();
(i = find_if(j, str_end, not1(pred))) != str_end;)
*output++ = string(i, j = find_if(i, str_end, pred));
}
int main() {
string input;
while (cin >> input) {
vector<string> words;
split(input, ptr_fun(::isspace), inserter(words, words.begin()));
copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
}
return 0;
}
There is no problem in the code you posted. There is a very obvious problem with the real code you linked to: is_space and space are member functions, and they cannot be called without an instance of Split2. This requirement doesn't make sense, though, so at least you should make those functions static.
(Actually it doesn't make much sense for split_v3 to be a member function either. What does having a class called Split2 achieve over having just a free function - possibly in a namespace?)
As requested:
class SplitV2 {
public:
void foo();
private:
struct space { bool operator() (char c) { return isspace(c); } };
struct not_space {
Split2::space space;
bool operator() (char c) { return !space(c); }
};
Use them with std::find_if(it, it2, space()) or std::find_if(it, it2, not_space().
Notice that not_space has a default constructed space as a member variable. It may be not wise to construct space in every call to bool not_space::operator() but maybe the compiler could take care of this. If the syntax for overloading operator() confuses you and you would like to know more about using structs as Predicates you should have a look at operator overloading and some guidelines to the STL.
Off hand, I would say it should probably be
i = str.find_if( ...
j = str.find_if( ...