简体   繁体   中英

Cannot compile C++ code on Linux, but can on Mac OS

For a project I am working on I wanted to try out the following program: http://sourceforge.net/projects/tirg/

It consists of two C++ files, but I cannot get it to compile on my laptop running Linux, while it compiles without any problem on the laptop of a friend of mine running Mac OS. I have no experience whatsoever in compiling C++ code, so it might be a beginners mistake.

The laptop running Mac OS that managed to compile the code without problems used the following g++ version:

i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

I tried compiling the C++ files using gcc, g++, clang and clang++ on my laptop running Arch LInux (64 bit) with the following versions:

gcc and g++ (GCC) 4.8.1 20130725 (prerelease)
clang version 3.3 (tags/RELEASE_33/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix

The error I get when compiling it is:

/usr/include/c++/4.8.1/bits/stl_vector.h:1240:36: error: no matching function for call to ‘std::vector<std::vector<unsigned char> >::_M_fill_assign(int&, int&)’
         { _M_fill_assign(__n, __val); }
                                    ^

Which was caused by this line of code:

std::vector<std::vector<trg::Rgb> > a(300, 255);

I have put the complete output of the compilation on pastebin: http://pastebin.com/m285PSrH

I also had problems when compiling OpenCV, so there is probably something wrong with my configuration. I think it is a versioning problem, a wrong version of the standard library for example.

Hopefully someone with more experience in C++ can point out what might have gone wrong. Thanks!

std::vector<std::vector<trg::Rgb> > a(300, 255);

has no sense. you wanted to write something like this:

//std::vector<std::vector<trg::Rgb> > a(picHeight, picWidth);
std::vector<std::vector<trg::Rgb> > a;
a.resize(picWidth);
{   
        std::vector<std::vector<trg::Rgb> >::iterator end = a.end();
        std::vector<std::vector<trg::Rgb> >::iterator it = a.begin();
        for(; it != end; ++it)
                it->resize(picHeight);
} 

you should correct each assign() in your code and each call to constructor of

vector<vector<XX> >

The i686-apple-darwin11-llvm-g++-4.2 does it wrong. You would get an 2D vector of vectors if an implicit conversion is possible. However [23.3.6.2]:

explicit vector(size_type n);

You may do:

struct Rgb {};
int main() {
    std::vector<Rgb> rgb_vector(255);
    std::vector<std::vector<Rgb> > a(300, rgb_vector);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM