a question and possibly found a bug

closed account (Dy7SLyTq)
so im writing some code and as is my style, i put prototypes at the beginning to use functions after main. why does the Str2Int have to be used before main and i cant make a prototype for it? i get a bunch of errors if i try. also i wrote the Log(int) function to try that, but called log() accidently, which i didnt write. why did it let me compile? it didnt give any output though. i am compiling with a bootstrapped g++ 4.8.1

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include  <fstream>
#include  <sstream>
#include    <regex>
#include   <random>
#include   <string>
#include   <vector>

using std::                    cout;
using std::                    cerr;
using std::                    endl;
using std::                     cin;
using std::                ifstream;
using std::           istringstream;
using std::   default_random_engine;
using std::uniform_int_distribution;
using std::                    bind;
using std::             regex_match;
using std::                   regex;
using std::                  cmatch;
using std::                  smatch;
using std::                  string;
using std::                  vector;

class Token
{
    string Name, Type;
    long Line, Column;

    public:
        Token(string name, string type, long line, long column)
            : Name(name), Type(type), Line(line), Column(column) {}

        void SetName   (string name) { Name   = name;   }
        void SetType   (string type) { Type   = type;   }
        void SetLine   (long line)   { Line   = line;   }
        void SetColumn (long column) { Column = column; }

        string GetName   () { return Name;   }
        string GetType   () { return Type;   }
        long   GetLine   () { return Line;   }
        long   GetColumn () { return Column; }
};

constexpr unsigned int Str2Int(const char *Line, int CurrentPos = 0)
{
    return !Line[CurrentPos] ? 5381 : (Str2Int(Line, CurrentPos + 1) * 33) ^ Line[CurrentPos];
}

void Log(int Line)
{
    static int Current = 1;

    cout<<"Log ("<< Current++ <<") @ Line: "<< Line << endl;
}

void Help    ();
void Version ();
void About   ();
void Lex     (vector<vector<string>>, vector<Token>);

int main(int argc, char *argv[])
{

    vector<string> Arguments(argv + 1, argv + argc);
    vector<string> FileList;

    for(auto &Counter : Arguments)
    {
        switch(Str2Int(Counter.c_str()))
        {
            case Str2Int("--help"):
                Help();
                break;

            case Str2Int("--version"):
                Version();
                break;

            case Str2Int("--about"):
                About();
                break;

            default:
                FileList.push_back(Counter);
        }
    }

    ifstream File;
    vector<vector<string>> FileContents(0);

    for(int Counter(0); Counter < FileList.size(); Counter++)
    {
        File.open(FileList[Counter]);
        FileContents.resize(FileContents.size() + 1);

        string Line;

        while(getline(File, Line))
            FileContents[Counter].push_back(Line);

        File.close();
    }

    vector<Token> TokenList;

    Lex(FileContents, TokenList);

    cout<< (char) 5381 << endl;
}

void Help()
{
}

void Version()
{
}

void About()
{
}

void Lex(vector<vector<string>> FileContents, vector<Token> TokenList)
{
}


it's probably because of the digit in the name of the function... also because 'counter' is undeclared in this scope...
Last edited on
> i get a bunch of errors if i try.
¿why have you not included such errors?

> also i wrote the Log(int) function to try that, but called log() accidently, which i didnt write.
> why did it let me compile?
I don't see such a thing in the code that you posted
http://www.cplusplus.com/reference/cmath/log/
somehow that header gets included. Because of C, there is a definition in the global namespace
what it is then?
what is it then?
closed account (Dy7SLyTq)
sorry i should have been more specific.
@aeon: what do you mean counter isnt declared in this scope?
@ne555: i just wanted to know why it wont let me do it. i dont care if i have to put the function on top. secondly, i removed all of the log calls because i only use them when im trying to find a bug
thirdly: that makes sense. thanks
Hello, constexpr?
closed account (Dy7SLyTq)
does that mean i cant prototype?
does that mean i cant prototype?


No. http://ideone.com/JhOrAj

It means you likely forgot to supply the constexpr specifier on either the declaration or definition. Hard to say though, since you didn't provide code/error messages.
Last edited on
> i just wanted to know why it wont let me do it
> i removed all of the log calls because i only use them when im trying to find a bug
If your snip does not contain the issues that you are complaining about ¿what's its purpose then?

@cire: try to use the function where it does matter for it to be a constant expression.
By instance, template parameter, array length, switch case.
@cire: try to use the function where it does matter for it to be a constant expression.

I thought it was obvious you needed the definition at the point of use for it to be treated as a constant expression. The error message is pretty clear for GCC in that case. I didn't consider it likely to be the source of the OP's woe.

Of course, the error message is pretty clear when the declaration/definition don't match too, so maybe it is.

Possibly he'll put us out of our misery and provide enough context to figure it out.

[edit: Although I see he is using it in a switch case. Shame on moi for skimming.]
Last edited on
closed account (Dy7SLyTq)
these are the errors im getting:

dtscode@dtscode-Latitude-E6410:~/Desktop/compiler$ ../gcc*/bin/g++ -std=c++11 main.cpp -o jade
main.cpp: In function 'int main(int, char**)':
main.cpp:40:34: error: 'constexpr unsigned int Str2Int(const char*, int)' used before its definition
             case Str2Int("--help"):
                                  ^
main.cpp:44:37: error: 'constexpr unsigned int Str2Int(const char*, int)' used before its definition
             case Str2Int("--version"):
                                     ^
main.cpp:48:35: error: 'constexpr unsigned int Str2Int(const char*, int)' used before its definition
             case Str2Int("--about"):
                                   ^
main.cpp: In function 'constexpr unsigned int Str2Int(const char*, int)':
main.cpp:67:68: error: default argument given for parameter 2 of 'constexpr unsigned int Str2Int(const char*, int)' [-fpermissive]
 constexpr unsigned int Str2Int(const char *Line, int CurrentPos = 0)
                                                                    ^
main.cpp:24:24: error: after previous specification in 'constexpr unsigned int Str2Int(const char*, int)' [-fpermissive]
 constexpr unsigned int Str2Int(const char*, int = 0);
                        ^
Topic archived. No new replies allowed.