Çözüldü C++ Header dosyasında "#include nested too deeply" hatası

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

Wuesty

Hectopat
Katılım
29 Haziran 2021
Mesajlar
578
Makaleler
1
Çözümler
4
Yer
ANKARA / KEÇİÖREN
Daha fazla  
Sistem Özellikleri
AMD Ryzen 7 3700x Sıvı Soğutmalı İşlemci | XFX RX 5600XT THICC III ULTRA 6GB VRAM Ekran Kartı | Geil Evo Spear AMD Ryzen Edition CL18 8x2 16 GB 3600 MHz Dual Channel Ram | Kingston A2000 Series SA2000M8/500G NVMe M2 SSD | Thermaltake 650W PSU | Thermaltake Versa T25 TG Kasa | Gigabyte B450M S2H Anakart | ASUS TUF GAMING VG277Q1A 165HZ 1MS 27" Monitör
Cinsiyet
Erkek
Meslek
Öğrenci
Arkadaşlar merhaba, oluşturduğum header dosyasında 3 tane "#include nested too deeply" hatası var, düzeltemiyorum. Yardımcı olur musunuz? Kod:

C++:
#include <time.h>
clock_t t0 = clock(); // read CPU time
/// do tasks ...
clock_t t1 = clock();
double cpu_time = (t1 - t0) / CLOCKS_PER_SEC;

#include <CPUclock.h>

CPUclock clock;
clock.init();
// perform tasks ...
double cpu_time = clock.getCPUtime();
...

/// perform more tasks
... double cpu_time2 = clock.getCPUtime();

// perform even more tasks
... double cpu_time3 = clock.getCPUtime();

class CPUclock
{
private:
 clock_t t0;

public:
 void init() { t0 = clock(); }
 double getCPUtime()
 {
 double t0_end = clock();
 double cpu = double((t0_end - t0) / CLOCKS_PER_SEC);
 t0 = clock_t(t0_end);
 return cpu;
 }
};

#ifndef CPUclock_H
#define CPUclock_H
#include <time.h> // clock function

#ifdef HAS_TMS
#include <sys/times.h> // tms struct
#endif

class CPUclock
{
private:
 clock_t t0;

#ifdef HAS_TMS
 tms t1, diff;
 double cpu_time, child_cpu_time;
#endif

public:
 void init();
 double getCPUtime();
};
#endif
 
Son düzenleyen: Moderatör:
Çözüm
Hocam vscode kullanıyosanız configle vs. alakalı olabilir, vscode kullanmadığım için tam hakim değilim maalesef. Direkt derlemek için terminali açıp .cpp dosyasının bulunduğu dizine gidin, sonrasında "g++ dosyaadi.cpp" komutunu çalıştırın (tırnaklar olmadan ve dosyaadi kısmını ismi neyse o yapın.)
Hocam bildiğim kadarıyla bu hata header'ların bir döngü içerisinde çok fazla kez include edilmesiyle gerçekleşiyor dolayısıyla header guardlardan birisi problemli olabilir, o nedenle belki macroda sorun olabilir diye düşündüm. "sys/times.h" bildiğim bir standart header değil belki onun içerisinde bir problem olabilir eğer HAS_TMS orada define ediliyorsa ve orada kontrol edilmeden yeniden include ediliyorsa belki problem olabilir ancak tam emin değilim.
 
Hocam bildiğim kadarıyla bu hata Header'ların bir döngü içerisinde çok fazla kez include edilmesiyle gerçekleşiyor dolayısıyla header guardlardan birisi problemli olabilir, o nedenle belki macroda sorun olabilir diye düşündüm. "sys/times.h" bildiğim bir standart header değil belki onun içerisinde bir problem olabilir eğer HAS_TMS orada define ediliyorsa ve orada kontrol edilmeden yeniden include ediliyorsa belki problem olabilir ancak tam emin değilim.

Kusura bakmayın biraz zahmetli olabilir belki de attığım kodu siz düzgün olabilecek şekilde düzenleyebilir misiniz acaba?
Bir de HAS_TMS ve sys/times.h ' den kaynaklı olduğunu düşünmüyorum.

Ve 3 hata daha var yazmamışım üste:

Redefinition of clock_t t0
Redefinition of clock_t t1
Redefinition of double cpu_time

Sadece ekteki hatalar var.
 

Dosya Ekleri

  • 1721328106309486811786771908888.jpg
    1721328106309486811786771908888.jpg
    335,9 KB · Görüntüleme: 22
Hocam tamamını header'a yazmamalısınız, header'da sadece definition'lar bulunmalı, ben ifndef'ten sonraki kısmı header olarak ayrı yazdınız sandım, bu şekilde sürekli olarak cpuclock.h'ı include ediyorsunuz. Şu şekilde yapın, ilk olarak bu cpuclock.h:

C++:
#ifndef CPUclock_H
#define CPUclock_H
#include <time.h> // clock function

#ifdef HAS_TMS
#include <sys/times.h> // tms struct
#endif

class CPUclock
{
private:
 clock_t t0;

public:
 void init() { t0 = clock(); }
 double getCPUtime()
 {
 double t0_end = clock();
 double cpu = double((t0_end - t0) / CLOCKS_PER_SEC);
 t0 = clock_t(t0_end);
 return cpu;
 }
};

#endif



Ardından da implementation'un bulunduğu bir source file oluşturun mesela test.cpp:

C++:
#include <CPUclock.h>

int main() {

    clock_t t0 = clock();
    clock_t t1 = clock();
    double cpu_time = (t1 - t0) / CLOCKS_PER_SEC;

    double cpu_time0;
    double cpu_time1;
    double cpu_time2;
    
    CPUclock clock;
    clock.init();
    
    cpu_time0 = clock.getCPUtime();
    cpu_time1 = clock.getCPUtime();
    cpu_time2 = clock.getCPUtime();

    return 0;
}
 
Hocam tamamını header'a yazmamalısınız, header'da sadece definition'lar bulunmalı, ben ifndef'ten sonraki kısmı header olarak ayrı yazdınız sandım, bu şekilde sürekli olarak cpuclock.h'ı include ediyorsunuz. Şu şekilde yapın, ilk olarak bu cpuclock.h:

C++:
#ifndef CPUclock_H
#define CPUclock_H
#include <time.h> // clock function

#ifdef HAS_TMS
#include <sys/times.h> // tms struct
#endif

class CPUclock
{
private:
 clock_t t0;

public:
 void init() { t0 = clock(); }
 double getCPUtime()
 {
 double t0_end = clock();
 double cpu = double((t0_end - t0) / CLOCKS_PER_SEC);
 t0 = clock_t(t0_end);
 return cpu;
 }
};

#endif



Ardından da implementation'un bulunduğu bir source file oluşturun mesela test.cpp:

C++:
#include <CPUclock.h>

int main() {

    clock_t t0 = clock();
    clock_t t1 = clock();
    double cpu_time = (t1 - t0) / CLOCKS_PER_SEC;

    double cpu_time0;
    double cpu_time1;
    double cpu_time2;
   
    CPUclock clock;
    clock.init();
   
    cpu_time0 = clock.getCPUtime();
    cpu_time1 = clock.getCPUtime();
    cpu_time2 = clock.getCPUtime();

    return 0;
}
Çok teşekkür ederim, ama bi de asıl kodun olduğu kısım sıkıntı çıkarıyor şimdi ona da bakabilir misiniz acaba:

#include <CPUclock.h>
#ifdef HAS_TMS
#include <unistd.h>
#endif

void CPUclock:: init ()
{
t0 = clock();
#ifdef HAS_TMS
times(&t1);
#endif
};

double CPUclock:: getCPUtime ()
{
double t0_end = clock();
double cpu_time_clock = double((t0_end - t0) / CLOCKS_PER_SEC);

#ifdef HAS_TMS
tms t2;
times(&t2);
diff.tms.utime = t2.tms.utime - t1.tms_utime;
diff.tms.stime = t2.tms.stime - t1.tms_stime;
diff.tms.cutime = t2.tms.cutime - t1.tms_cutime;
diff.tms.cstime = t2.tms.cstime - t1.tms_cstime;
double clock_ticks_per_sec = sysconf(_SC_CLK_TCK); // Linux
cpu_time_clock = double(diff.tms_utime + diff.tms_stime) \
/clock_ticks_per_sec;
child_cpu_time =
double(diff.tms_cutime + diff.tms_cstime) / clock_ticks_per_sec;

// update t1 such that next getCPUtime() gives new difference:
times(&t1);
#endif
t0 = clock_t(t0_end);

return cpu_time_clock;
};

double CPUclock::getSystemTime()
{
#ifdef HAS_TMS
return double(diff.tms_stime) / sysconf(_SC_CLK_TCK);
#endif
};
 

Dosya Ekleri

  • 17213298416614174864763175829295.jpg
    17213298416614174864763175829295.jpg
    262,2 KB · Görüntüleme: 23
Çok teşekkür ederim, ama bi de asıl kodun olduğu kısım sıkıntı çıkarıyor şimdi ona da bakabilir misiniz acaba:

#include <CPUclock.h>
#ifdef HAS_TMS
#include <unistd.h>
#endif

void CPUclock:: init ()
{
t0 = clock();
#ifdef HAS_TMS
times(&t1);
#endif
};

double CPUclock:: getCPUtime ()
{
double t0_end = clock();
double cpu_time_clock = double((t0_end - t0) / CLOCKS_PER_SEC);

#ifdef HAS_TMS
tms t2;
times(&t2);
diff.tms.utime = t2.tms.utime - t1.tms_utime;
diff.tms.stime = t2.tms.stime - t1.tms_stime;
diff.tms.cutime = t2.tms.cutime - t1.tms_cutime;
diff.tms.cstime = t2.tms.cstime - t1.tms_cstime;
double clock_ticks_per_sec = sysconf(_SC_CLK_TCK); // Linux
cpu_time_clock = double(diff.tms_utime + diff.tms_stime) \
/clock_ticks_per_sec;
child_cpu_time =
double(diff.tms_cutime + diff.tms_cstime) / clock_ticks_per_sec;

// update t1 such that next getCPUtime() gives new difference:
times(&t1);
#endif
t0 = clock_t(t0_end);

return cpu_time_clock;
};

double CPUclock::getSystemTime()
{
#ifdef HAS_TMS
return double(diff.tms_stime) / sysconf(_SC_CLK_TCK);
#endif
};

Hocam header'larda sadece function prototype'ları da verebilirsiniz tabii. Header dosyasını şu şekilde değiştirin:

C++:
#ifndef CPUclock_H
#define CPUclock_H
#include <time.h> // clock function

#ifdef HAS_TMS
#include <sys/times.h> // tms struct
#endif

class CPUclock
{
private:
 clock_t t0;

#ifdef HAS_TMS
 tms t1, diff;
 double cpu_time, child_cpu_time;
#endif

public:
 void init();
 double getCPUtime();
 double getSystemTime();
};

#endif

Source dosyasını da şu şekilde değiştirin:

C++:
#include <CPUclock.h>
#ifdef HAS_TMS
    #include <unistd.h>
#endif

void CPUclock:: init () {
    t0 = clock();
#ifdef HAS_TMS
    times(&t1);
#endif
};

double CPUclock:: getCPUtime () {
    double t0_end = clock();
    double cpu_time_clock = double((t0_end - t0) / CLOCKS_PER_SEC);

    #ifdef HAS_TMS
        tms t2;
        times(&t2);
        diff.tms.utime = t2.tms.utime - t1.tms_utime;
        diff.tms.stime = t2.tms.stime - t1.tms_stime;
        diff.tms.cutime = t2.tms.cutime - t1.tms_cutime;
        diff.tms.cstime = t2.tms.cstime - t1.tms_cstime;
    
        double clock_ticks_per_sec = sysconf(_SC_CLK_TCK);
        cpu_time_clock = double(diff.tms_utime + diff.tms_stime) / clock_ticks_per_sec;
        child_cpu_time = double(diff.tms_cutime + diff.tms_cstime) / clock_ticks_per_sec;
        times(&t1);
    #endif

     t0 = clock_t(t0_end);

     return cpu_time_clock;
};

double CPUclock::getSystemTime() {
    #ifdef HAS_TMS
        return double(diff.tms_stime) / sysconf(_SC_CLK_TCK);
    #endif
};

int main() {

    clock_t t0 = clock();
    clock_t t1 = clock();
    double cpu_time = (t1 - t0) / CLOCKS_PER_SEC;

    double cpu_time0;
    double cpu_time1;
    double cpu_time2;
 
    CPUclock clock;
    clock.init();
 
    cpu_time0 = clock.getCPUtime();
    cpu_time1 = clock.getCPUtime();
    cpu_time2 = clock.getCPUtime();

    return 0;
}

Ek olarak bu class'ı farklı bir source dosyasında da kullanacaksanız o source dosyasını implementation'un bulunduğu dosya ile birlikte derlemelisiniz.
 
Hocam Header'larda sadece function Prototype'ları da verebilirsiniz tabii. Header dosyasını şu şekilde değiştirin:

C++:
#ifndef CPUclock_H
#define CPUclock_H
#include <time.h> // clock function

#ifdef HAS_TMS
#include <sys/times.h> // tms struct
#endif

class CPUclock
{
private:
 clock_t t0;

#ifdef HAS_TMS
 tms t1, diff;
 double cpu_time, child_cpu_time;
#endif

public:
 void init();
 double getCPUtime();
 double getSystemTime();
};

#endif

Source dosyasını da şu şekilde değiştirin:

C++:
#include <CPUclock.h>
#ifdef HAS_TMS
 #include <unistd.h>
#endif

void CPUclock:: init () {
 t0 = clock();
#ifdef HAS_TMS
 times(&t1);
#endif
};

double CPUclock:: getCPUtime () {
 double t0_end = clock();
 double cpu_time_clock = double((t0_end - t0) / CLOCKS_PER_SEC);

 #ifdef HAS_TMS
 tms t2;
 times(&t2);
 diff.tms.utime = t2.tms.utime - t1.tms_utime;
 diff.tms.stime = t2.tms.stime - t1.tms_stime;
 diff.tms.cutime = t2.tms.cutime - t1.tms_cutime;
 diff.tms.cstime = t2.tms.cstime - t1.tms_cstime;

 double clock_ticks_per_sec = sysconf(_SC_CLK_TCK);
 cpu_time_clock = double(diff.tms_utime + diff.tms_stime) / clock_ticks_per_sec;
 child_cpu_time = double(diff.tms_cutime + diff.tms_cstime) / clock_ticks_per_sec;
 times(&t1);
 #endif

 t0 = clock_t(t0_end);

 return cpu_time_clock;
};

double CPUclock::getSystemTime() {
 #ifdef HAS_TMS
 return double(diff.tms_stime) / sysconf(_SC_CLK_TCK);
 #endif
};

int main() {

 clock_t t0 = clock();
 clock_t t1 = clock();
 double cpu_time = (t1 - t0) / CLOCKS_PER_SEC;

 double cpu_time0;
 double cpu_time1;
 double cpu_time2;

 CPUclock clock;
 clock.init();

 cpu_time0 = clock.getCPUtime();
 cpu_time1 = clock.getCPUtime();
 cpu_time2 = clock.getCPUtime();

 return 0;
}

Ek olarak bu Class'ı farklı bir Source dosyasında da kullanacaksanız o Source dosyasını implementation'un bulunduğu dosya ile birlikte derlemelisiniz.

Teşekkür ederim işime yaradı ama Debug dediğim zaman otomatik .exe dosyası oluşmuyor ve g++.exe terminated with exit code -1 hatası alıyorum. Kusura bakmayın uğraşırıyorum sizi de gece gece ama
 
Hocam vscode kullanıyosanız configle vs. alakalı olabilir, vscode kullanmadığım için tam hakim değilim maalesef. Direkt derlemek için terminali açıp .cpp dosyasının bulunduğu dizine gidin, sonrasında "g++ dosyaadi.cpp" komutunu çalıştırın (tırnaklar olmadan ve dosyaadi kısmını ismi neyse o yapın.)
 
Çözüm

Technopat Haberler

Geri
Yukarı