1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex.h>

using namespace std;

string input;

int main()
{   
    //Input .ply file
    ifstream inputFile ("input.ply");


    //input file is read
    if (inputFile.is_open())
    {
        int i = 1;  //Line iterator
        int vertices = 0;
        int faces = 0;

        vector<float> anatomy;
        vector<float> normals;
        vector<int> triangles;

        string a,line;
        getline(cin,line);

        while (inputFile.good())
        {
            //Take number from line 4, set as variable "vertices"
            if (i == 4)
            {
                regex pattern("[^0-9]+");
                getline (inputFile,line);              
                vertices = show_match(line, pattern);
            }

            //Take number from line 11, set as variable "triangles"
            if (i == 11)
            {
                regex pattern("[^0-9]+");
                getline (inputFile,line);              
                faces = show_match(line, pattern);
            }

            if (i == 13)
            {
                i++;
                break;
            }

            i++;

        }

        waitKey(0);
}

else
{
    cout << "Cannot read mesh, please try again." << endl;
}

return 0;

}

Just trying to read from a string and extract the integer, so I have added the regex header and other files in order to use regular expressions. I'm using Dev-C++ and have extracted the files for regex into their respective lib, bin and include folders in "C:\Dev-Cpp", but I am still receiving the following error when I attempt to compile my program:

'regex' undeclared (first use this function)

Kirk
  • 16,182
  • 20
  • 80
  • 112
user2136754
  • 107
  • 1
  • 3
  • 9
  • 3
    Where did `regex.h` come from? – chris Apr 03 '13 at 21:32
  • use `#include ` assuming you have C++11 support. –  Apr 03 '13 at 21:37
  • 3
    If you're using GCC, it doesn't support C++11's regex. – chris Apr 03 '13 at 21:37
  • http://sourceforge.net/projects/mingw/files/Other/UserContributed/regex/mingw-regex-2.5.1/ Here – user2136754 Apr 03 '13 at 21:38
  • When I used `#include ` (without the .h) it said: *"regex: no such file or directory"* When I added the ".h" this compile error disappeared – user2136754 Apr 03 '13 at 21:41
  • Well, without the .h is the standard C++11 header. I don't know whether that one is supposed to be it, but with a .h randomly added, or a completely different one. – chris Apr 03 '13 at 21:43
  • 1
    is very probably the POSIX header providing the C regular expressions API. Nothing to do with C++11 but could be used instead, if used as per the C API. – Mike Kinghan Apr 03 '13 at 22:00
  • 1
    Is Dev-C++ not C++11 compatible? – user2136754 Apr 03 '13 at 22:00
  • 2
    Dev-C++ is an IDE, not a compiler. It's the compiler that is or is not C++11 compatible, and this compatibility is a matter of degree. I don't know what your compiler is but it will compile C as well as C++ and will be provided with C headers and libraries as well as C++ ones. I believe you have mistaken a C regex API header for a C++ regex class header. – Mike Kinghan Apr 03 '13 at 22:07
  • 1
    **[`while (stream.good())` is wrong](http://stackoverflow.com/q/5605125/560648)** - which resource instructed you to do that? – Lightness Races in Orbit Apr 03 '13 at 22:16
  • I'm rather inexperienced with C++, I found 'while (stream.good())' here: http://www.cplusplus.com/doc/tutorial/files/ – user2136754 Apr 03 '13 at 22:18
  • 1
    Well, now you know which site to distrust (read the linked explanation). – R. Martinho Fernandes Apr 03 '13 at 22:19
  • What if you turn on C++11 via project option or compiler setting? – MikeCAT Jun 06 '16 at 22:41

1 Answers1

0

You will get this error msg from the compiler if your statement #include <regex.h> is not reached, for example if you used conditional includes.

Make sure that #include <regex.h> is actually reached. I got this error message too when I accidentally excluded the include by using a conditional include for and then trying the code with .

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424