No Matching Function for Call to...

Hello guys! I been having some trouble with these codes. I need to make a cpp file that takes a .h file and do the rest from there. I'm using Vocareum but the problem is that I keep getting errors related to a function not matching.I have no idea how to fix it. There is no main.cpp file, since I haven't started it yet. I will have to use both the lexeme.h file and getToken.cpp in the main.cpp file for the final results. I just need to get the getToken.cpp file working first.

Below are the errors I keep getting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
getToken.cpp: In function ‘Tok getNextToken(std::istream&, int&)’:                                                                                                                                                                                                                                                                                     
getToken.cpp:114:39: error: no matching function for call to ‘Tok::Tok(Token, int&)’                                                                                                                                                                                                                                                                   
                 return Tok(ICONST,lnum);   


getToken.cpp:126:39: error: no matching function for call to ‘Tok::Tok(Token, int&)’                                                                                                                                                                                                                                                        
                 return Tok(SCONST,lnum); 


getToken.cpp:153:28: error: no matching function for call to ‘Tok::Tok(getNextToken(std::istream&, int&)::TokState, int&)’                                                                                                                                                                                                                             
       return Tok(BEGIN,lnum);  


getToken.cpp:159:31: error: no matching function for call to ‘Tok::Tok(Token, int&)’                                                                                                                                                                                                                                                                   
         return Tok(ICONST,lnum);  



Below is the code for a lexeme .h file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef LEX_H_
#define LEX_H_

#include <string>
#include <iostream>
using std::string;
using std::istream;
using std::ostream;

enum Token {
	
	//integer and string constant
	ICONST, SCONST,

        BEGIN, DONE
};

class Tok {
	Token	token;
	string	str;
	int	lnum;

public:
	Tok() {
		token = ERR;
		lnum = -1;
	}
	Tok(Token token, string str, int line) {
		this->token = token;
		this->str = str;
		this->lnum = line;
	}

	bool operator==(const Token token) const { return this->token == token; }
	bool operator!=(const Token token) const { return this->token != token; }

	Token	GetToken() const { return token; }
	string	GetStr() const { return str; }
	int	GetLinenum() const { return lnum; }
};

extern ostream& operator<<(ostream& out, const Tok& tok);

extern Tok getNextToken(istream& in, int& linenum);

#endif /* LEX_H_ */ 



The code below for the getToken.cpp file I also wrote the line number where the error is shown.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "lex.h"
#include <cctype>
#include <iostream>
using namespace std;

Tok
getNextToken(istream& in, int& linenum)
{
 
	enum TokState {
	    BEGIN,
  	  	INID,
   	 	ININT,
   	 	INSTRING,
   	 	INCOMMENT,
   	 	ONESLASH
	};

    TokState lexState = BEGIN;
	string lexeme;
	char ch;
    
 
    while (in.get(ch)) {
 
        switch (lexState) {
            case TokState::BEGIN:
                if (ch=='\n') {
                    linenum++;
                }           
                if (isspace(ch)) {
                   continue;
                }
                lexeme = ch;
     
                break;
              
            case ININT:
                if (isdigit(ch)) {
                    lexeme += ch;
                    continue;
                }
              
                in.putback(ch);
                return Tok(ICONST,linenum);  //this is line 114
                break;
              
            case INSTRING:
                lexeme += ch;
                if (ch == '\n') {
                    return Tok(DONE,"\n",linenum);
                }
                if (ch != '"') {
                    continue;
                }
              
                return Tok(SCONST,linenum);  //this is line 136
                break;
              
            case INCOMMENT:
                if (ch != '\n') {
                    continue;
                }
                lexState = TokState::BEGIN;
                break;
              
            default:
                cout << "Error!" << endl;
                exit(1);
                break;
        }
    }
 
    if(lexState == TokState::BEGIN){  //this is line 153
     	return Tok(BEGIN,linenum);   
    }
  
    else(lexState == ININT) {
        return Tok(ICONST,linenum);  //this is line 159
    }
}
Last edited on
Well, you don't have a ctor that takes two arguments.
So either use the one that takes three (maybe with an empty string "") or make one that takes two.
Tok(Token token, string str, int line)
if you want to construct a `Tok' object you need to provide a Token, a string and a number
three things, count them, three, not two, three

1
2
3
Tok(SCONST,linenum);
Tok(BEGIN,linenum);
Tok(ICONST,linenum);
¿how many are you providing?
¿how do you expect that to work?
The problem with that is that I don't know what to put for the third. I see there are two. For example, if I would have had a
 
Tok(MINUS,"-",linenum)

that how I would have it. I don't know what to do with those.
ICONST is an integer constant.
SCONST is a string constant.
BEGIN is just a keyword which is a token.
@dutch that only work for
 
Tok(Done, "", linenum)

where DONE is for when completed (EOF), must return this token.
For line 136,
Tok(SCONST, linenum)
Do I have to use
Tok(SCONST,lexeme,linenum)
since lexeme is declared as a string?

What would I use for Tok(ICONST,linenum)?

I believe that linenum is the third thing, the integer for the token. You can see have it as a parameter in the function from the second piece of codes.
Last edited on
It seems to me that you don't really understand what the token contains. Let's take a step back and talk about that.
1
2
3
4
5
enum Token {
	//integer and string constant
	ICONST, SCONST,
        BEGIN, DONE
};

Okay, so you have 4 types of tokens - integer constant, string constant, BEGIN, and DONE. BEGIN and DONE probably represent the start and end of the input file.

Now suppose you read an integer constant. You might want to assign that to a variable, or print it out, or add it to some other integer constant. To do that, you need more than the type of the token, you need the actual value. Is the integer 17? 833? -123543? You need to store the value.

So now we have two properties of the token - it's type and its value.

A third property that's very handy for error messages is the line number where a token appears in the input file. This is because sometimes you don't detect an error in the syntax until several lines later.

So lets document the fields in the declaration:
1
2
3
4
5
6
class Tok {
	Token	token;    // type of token
	string	str;      // actual value of the token
	int	lnum;     // input line number where the token occurred.
	...
};


Given this, it should be clear that yes, the second argument to the Tok() constructor should be lexeme
Gotcha, thanks. So, I'm getting this error now and I don't know why.
1
2
3
4
5
6
7
8
9
10
11
12
13
//The code producing the error
Tok token = getNextToken(*in,lexeme);

if(token.GetToken() == DONE)
{
......
}


error: invalid initialization of reference of type ‘int&’ from expression of type ‘std::__cxx11::string {aka std::__cxx11::basic_str
ing<char>}’             
            Tok token = getNextToken(*in, lexeme);  


At first I didn't declared it and it was showing an error because "lexeme" wasn't declared.
I thought it was already declared in the lex.h file.
Then after I declared it as, string lexeme;
It is now showing this new error.

The error is also showing a note:
1
2
 note: in passing argument 2 of ‘Tok getNextToken(std::istream&, int&)’                                                                
 getNextToken(istream& in, int& linenum) 


If I changed lexeme to linenum then it does compile but nothing happens. Non of the returns actually returns from the test cases.
Last edited on
I got it working. Thanks for the help guys!
Topic archived. No new replies allowed.