c ++:main()问题? - 在'{'标记之前不允许使用函数声明

时间:2011-08-04 18:57:38

标签: c++

我遇到两个编译错误:

/home/sater/projects/MotionManager/videoProcess.cpp:11:1: error: a function-definition is not allowed here before ‘{’ token
/home/sater/projects/MotionManager/main.cpp:85:1: error: expected ‘}’ at end of input

我检查确保所有括号和括号都匹配,但无法找到此问题的根。你能看一下我的代码,看看我是否误解了什么?我是c + +的新手,我主要来自网络编程和脚本编写经验,但我是通过做某种人来学习的,所以请原谅因为没有“读我的书”而导致的任何错误,因为我没有。一个简单的解释或链接到有关我的失败的更多信息将是有帮助的。

我调用processVideoFile函数的方式似乎一定存在问题,但我无法弄清楚我做错了什么。感谢您花时间看一看。

的main.cpp

    #include <iostream>
#include <stdio.h>
#include <string>
#include <time.h>
#include "parseMotion.cpp"
#include "resumepause.cpp"
#include "videoProcess.cpp"

using namespace std;

bool DEBUG = true;

int main(int argc, char **argv) {
    //Useful directory strings
    string camDir [3];
    camDir[0]= "cam01(Outside)/";
    camDir[1]= "cam02(Upper)/";
    camDir[2]= "cam03(Lower)/";

    string latestWD = "/Store/SecurityStorage/LatestVideos/";
    string archiveWD = "/Store/SecurityStorage/VideoArchive/";

    int threadStatus [3];
    threadStatus[0] = 0;
    threadStatus[1] = 0;
    threadStatus[2] = 0;
    //-50 paused
    //-1 couldn't connect initially to camera
    //0 Not yet started
    //1 Started successfully
    //2 lost connection to cam - retrying

    deque<string> fileDeque;
    deque<int> fileTimingDeque;



    int exitTrig = -1;
    int counter = 0;

    FILE *MotionIOStream = popen("motion 2>&1", "r");
    if(!MotionIOStream){
      exitTrig = 1;
    }
    while( exitTrig < 0 )
    {      
      //Set maximum length, fgets: get one line
      char buffer[1024]; 
      string lineOutput;
      lineOutput = fgets(buffer, sizeof(buffer), MotionIOStream);

      //check mysqldb for pause/resume
      int rpResult;
      rpResult = checkSchedule(threadStatus);
      if(rpResult != 0)
      {
    return rpResult;
      }

      //process video - fork
      int pid;
      pid = processVideoFiles(fileDeque, fileTimingDeque);
      if(pid == 0)
      {
    return 0;
      }

      //cout << "Starting to parse" << endl;
      parseMotionOutput(lineOutput, threadStatus, fileDeque, fileTimingDeque);

      counter++;
      //[3] Thread 3 started
      if( counter > 20 )
      {
    exitTrig = 2;
    pclose(MotionIOStream);
    system("killall motion");
    return 0;
      }
    }
    //Handle error returns here.
    //case 1: "Couldn't start motion."
    //case 2: ""
    return 0;
}

这是videoProcess.cpp:

#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <boost/xpressive/xpressive_static.hpp>

using namespace boost::xpressive;

int processVideoFiles( std::deque<std::string> &fileDeque, std::deque<int> &fileTimingDeque )
{

  std::time_t secondsNow;
  secondsNow = std::time(NULL);

  int nextFileSeconds = fileTimingDeque.front();
  int diff = secondsNow - nextFileSeconds;
  if( diff > 120)
  {
    //Should fork
    int pid = fork();
    if(pid == 0){
      //Is child
      std::string filePath = fileDeque.front();
      mark_tag wd(1), camDir(2), dateDir(3), fileName(4);
      // /Store/SecurityStorage/LatestVideos/cam03(Lower)/2011-07-21/01-20110721112236.avi
      sregex pathRegex =  (wd= "/Store/SecurityStorage/LatestVideos/") >> (camDir= ("cam0" >> range('1', '3') >> '/') ) >> (dateDir= ( repeat<4,4>(range('0', '9')) >> '-' >> '0' | '1' >> range( '0', '9' ) >> '/') ) >> (fileName= ( repeat<2,2>(range('0','9')) >> '-' >> repeat<14,14>(range('0','9')) >> '.' >> "avi" ) ) ;
      smatch pathWhat;
      if( regex_search(filePath, pathWhat, pathRegex) )
      {
    std::cout << "The working directory for this file is:" << pathWhat[wd] << std::endl;
    std::cout << "The camera directory for this file is:" << pathWhat[camDir] << std::endl;
    std::cout << "The date directory for this file is:" << pathWhat[dateDir] << std::endl;
    std::cout << "The filename of this file is:" << pathWhat[fileName] << std::endl;
      }else
      {
    std::cout << "Error: Couldn't match the file path regex." << std::endl;
    return 0;
      }

      //mplayer $filepath -vo jpeg -ao null -frames 25
      std::string commandString = "mplayer " + filePath + " -vo jpeg -ao null -frames 25";
      const char* commandChar = commandString.c_str();

      int commandResult = system(commandChar);
      if(commandResult != 0)
      {
    std::cout << "Couldn't create jpeg files for some reason" << endl;
      }else
      {
    return 0;
      }

      //Last steps
      fileDeque.pop_front();
      fileTimingDeque.pop_front();
    }
      return pid;
  }else
  {
    return 1;
  }
}

4 个答案:

答案 0 :(得分:1)

main定义似乎是正确的,因此其中一个包含的文件中必须存在错误。不建议使用.cpp文件,顺便说一句,不这样做会使这些错误更容易调试。

答案 1 :(得分:1)

正如larsmans建议的那样,这是由前一个标题上的一些陈旧定义引起的。尝试删除它们,只留下其中一个,看看哪一个引入了错误。另外,另一个技巧可能是直接使用cpp生成一个大文件,然后将其编辑成带有语法突出显示的编辑器,以查看错过括号的位置等等:

cpp -Iinclude_dirs main.cpp&gt; output_file.cpp

答案 2 :(得分:0)

好像你忘了把;放在某个类的声明之后,或忘了放一个匹配的}。该错误很可能出现在您未显示的某个头文件中。

答案 3 :(得分:0)

标题<deque>包含在哪里?

此外,您是否可以添加其他两个文件,尤其是resumepause.cpp文件