2010-03-22 114 views
0

我是一个初学者,我有一个问题:这个C++代码有什么问题?

这个代码犯规编译:

main.cpp中:

#include <stdlib.h> 
#include "readdir.h" 
#include "mysql.h" 
#include "readimage.h" 


int main(int argc, char** argv) { 
    if (argc>1){ 
    readdir(argv[1]); 
    // test(); 
    return (EXIT_SUCCESS); 
    } 
    std::cout << "Bitte Pfad angeben !" << std::endl ; 
    return (EXIT_FAILURE); 
} 

readimage.cpp

#include <Magick++.h> 
#include <iostream> 
#include <vector> 

using namespace Magick; using namespace std; 

void readImage(std::vector<string> &filenames) { 
    for (unsigned int i = 0; i < filenames.size(); ++i) { 
     try { 
      Image img("binary/" + filenames.at(i)); 

      for (unsigned int y = 1; y < img.rows(); y++) { 
       for (unsigned int x = 1; x < img.columns(); x++) { 
        ColorRGB rgb(img.pixelColor(x, y)); 
        // cout << "x: " << x << " y: " << y << " : " << rgb.red() << endl; 
       } 
      } 
      cout << "done " << i << endl; 
     } catch (Magick::Exception & error) { 
      cerr << "Caught Magick++ exception: " << error.what() << endl; 
     } 
    } } 

readimage.h

#ifndef _READIMAGE_H 
#define _READIMAGE_H 

#include <Magick++.h> 
#include <iostream> 
#include <vector> 
#include <string> 

using namespace Magick; 
using namespace std; 

void readImage(vector<string> &filenames) 


#endif /* _READIMAGE_H */ 

如果要使用此代码进行编译:

g++ main.cpp Magick++-config --cflags --cppflags --ldflags --libs readimage.cpp

我收到此错误信息:

main.cpp:5: error: expected initializer before ‘int’

我不知道,为什么呢? :(

有人可以帮我吗?:)

+1

无关:**永远不要有'在你的头文件使用namespace' ** – 2010-03-22 16:25:28

回答

7

readimage.h,你是你的readImage函数声明之后缺少一个分号。

+0

噢人...如何来,错误是主要的?! :D – 2010-03-22 15:49:31

+0

@ mr.bio:实际上,错误是在#include之后;这表明错误发生在您包含的文件的末尾。我承认这发生在我身上不少次。 – Gorpik 2010-03-22 15:53:02

+0

因为* readimage.h *是要包含在* main.cpp *中的最后一个文件。 '#include ...'是一个直接的文本替换。 '#inc' -ed文件中的错误会影响编译'#inc' -ed文件... – 2010-03-22 15:53:36

3

在readImage.h中,在readImage函数原型之后缺少分号。

3

此函数声明:

void readImage(vector<string> &filenames) 

缺少在最后一个分号。一个无关的问题 - 你的包括后卫名称:

#ifndef _READIMAGE_H 

是非法的。以下划线和大写字母开头的名称在C++中保留 - 您不能自己创建这些名称。

而且在你的循环:

for (unsigned int y = 1; y < img.rows(); y++) { 

你确定你应该在1在开始进行循环,而不是在零?

+0

NetGuard自动生成的掩护 – 2010-03-22 15:50:52

+0

@mr .bio真的吗?然后netbeans被破坏。但我怀疑这只是一些需要修复的文本模板, – 2010-03-22 15:51:45

+0

谢谢..对于Loop建议..修复;) – 2010-03-22 15:57:51

1

;缺少在readimage.h

结束以来,main.cpp是预处理的第一,它发现错误在readimage.h最后一行并显示在main.cpp

main.cpp:5: error: expected initializer before ‘int’

int之前发生的错误
5

第一次编译时应该做的第一件事情之一就是一次尝试一段代码。不要把一堆代码放在一起,并希望它编译。相反,一次只取一个片段。所以在这里,你可能会注释掉你的包含以及你期望在这些文件中使用这些东西的代码。

乍一看,它看起来像

void readImage(vector<string> &filenames) 

缺少在该行的最后一个分号,因为你宣布它。

1

你只需要一个分号readImage声明中readimage.h结束:

void readImage(vector<string> &filenames);