2016-04-14 17 views
-3

Bu hatayla ilgili başka sorulara da baktım, ama şu ana kadarki problemimin cevabını neye benzediğini buldum. İki sınıfım var Message ve ColorString. Eski bir yöntemde ben ColorStringC++ statik olmayan veri üyesinin geçersiz kullanımı

message.hpp 

#ifndef __MESSAGE__HPP 
#define __MESSAGE__HPP 
#if defined __linux || defined __APPLE__ 
#define UNIXLIKE 
#endif 
//////////////////////////////////////////////////////////////////////////////// 
// Message helps organize error and warning messages       // 
//////////////////////////////////////////////////////////////////////////////// 
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include "color.hpp" 
//////////////////////////////////////////////////////////////////////////////// 
//         MESSSAGE         // 
//////////////////////////////////////////////////////////////////////////////// 
class Message 
{ 
//////////////////////////////////////////////////////////////////////////////// 
public: // types 
//////////////////////////////////////////////////////////////////////////////// 
    typedef COLOR::ColorString           colstr; 
    typedef COLOR::ColorID            color; 
//////////////////////////////////////////////////////////////////////////////// 
public: // methods 
//////////////////////////////////////////////////////////////////////////////// 
    Message(std::ostream& o = std::cerr) 
    : o(std::cerr) 
    { 

    } 
//////////////////////////////////////////////////////////////////////////////// 
    Message(const std::string& message, 
     const std::string& label ="", 
     const std::string file="", 
     const int line = -1, 
     std::ostream& o = std::cerr, 
     const color c = COLOR::RED 
     ) 
    : o(std::cerr) 
    { 

    } 
//////////////////////////////////////////////////////////////////////////////// 
    friend std::ostream& operator<<(std::ostream& o, Message& m) 
    { 
     #ifdef UNIXLIKE 
     colstr lbl(label, c); 
     colstr msg(message); 
     colstr ln(linestr); 
     colstr fl(file); 
     #else 
    std::string lbl(label); 
     std::string msg(message); 
     std::string ln(linestr); 
     std::string fl(file); 
     #endif 
    o << fl << ln; 
    if (fl.size() > 0 || ln.size() > 0) o << ": "; 
    o << lbl << " " << msg << "\n"; 
    o.flush(); 
    return o; 
    } 
//////////////////////////////////////////////////////////////////////////////// 
private: // methods 
//////////////////////////////////////////////////////////////////////////////// 
    void init(const std::string& message, 
      const std::string& label = "", 
      const std::string file="", 
      const int line = -1, 
      std::ostream& o = std::cerr, 
      const color c = COLOR::RED) 
    { 
    this->message = message; 
    this->label = label; 
    this->file = file; 
    this->line = line; 
    this->c = c; 

    if (this->line > -1) 
    { 
     std::stringstream ss; 
     ss << this->line; 
     ss >> linestr; 
    } 

    if (this->file.size() > 0) 
    { 
     this->file = this->file+":"; 
     if (this->line > -1) 
     { 
     this->file = this->file+linestr; linestr=""; 
     } 
    } 
    } 
//////////////////////////////////////////////////////////////////////////////// 
private : // fields 
//////////////////////////////////////////////////////////////////////////////// 
    std::string label; 
    std::string message; 
    std::string file; 
    int line; 
    std::ostream& o; 
    color c; 
    std::string linestr; 
}; 
#endif 

kurucusuna Message üyelerini geçerek ikincisi çeşitli örneklerini yapmak ColorString yapıcısı şu şekildedir:

/** 
* @brief constructs a ColorString with color @p c and the string 
*  @p s. 
* @param s string to wrap 
* @param c color to print @p s in 
* @param bold determines whether @p s will be printed bold 
*/ 
ColorString(str s, ColorID c=DEF, bool bold=1) 
:string(s), 
color(c), 
bold(bold) 
{ 
} 

bölüm hataya neden şudur:

#ifdef UNIXLIKE 
    colstr lbl(label, c); 
    colstr msg(message); 
    colstr ln(linestr); 
    colstr fl(file); 
    #else 

hatalar:

message.hpp:58:20: error: invalid use of non-static data member 'label' 
     colstr lbl(label, c); 
        ^~~~~ 
message.hpp:58:27: error: invalid use of non-static data member 'c' 
     colstr lbl(label, c); 
         ^
message.hpp:59:20: error: invalid use of non-static data member 'message' 
     colstr msg(message); 
        ^~~~~~~ 
message.hpp:60:19: error: invalid use of non-static data member 'linestr' 
     colstr ln(linestr); 
        ^~~~~~~ 
message.hpp:61:19: error: invalid use of non-static data member 'file' 
     colstr fl(file); 

Burada sorun nedir?

+1

Sorununuzla ilgisi yoktur, ancak kodunuzda çift alt çizgi içeren semboller kullanmayın. Bu semboller, "uygulama" (yani, derleyici ve standart kütüphane) için ayrılmıştır. Daha fazla bilgi için [bu soru ve cevaplar] bölümüne bakınız (http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). –

+1

Sorununuza gelince, en çok endişelenen ayrıştırmayla ilgili olabilir (https://en.wikipedia.org/wiki/Most_vexing_parse) ve derleyici bunun yerine işlev prototiplerini bildirdiğinizi düşünüyor. Ör. colstr lbl = colstr (label, c); 'yerine. –

+0

Teşekkürler, ama aynı hata. 'message.hpp: 58: 29: hata: statik olmayan veri üyesi 'etiket' geçersiz kullanımı colstr lbl = colstr (etiket, c);' –

cevap

2

Sorun, bir friend işlevini tanımlamanızdır. Sınıfta "satır içi" tanımladığınızda bile, bunlar hala üye olmayan işlevlerdir ve üyelerine erişmek için bir nesne örneğine gereksinim duyarlar.

Yapmanız gereken ör.

colstr lbl(m.label, m.c); 
+0

ah, teşekkürler. –

İlgili konular