I'm new in C++, and I have difficulty in my project.
Super.h
#pragma once
#include <iostream>
float *list=new float[10];
class Super{
public:
void function(){}
};
A.h
#pragma once
#include "Super.h"
class A{
public:
void function();
};
A.cpp
#include "A.h"
void A::function(){
std::cout<<"Working A!"<<std::endl;
}
main.cpp
#include "A.h"
int main(int argc, const char * argv[]) {
A a;
a.function();
return 0;
}
This code is not compiled,(clang: error: linker command failed with exit code 1 (use -v to see invocation)) and I found if I delete
float *list=new float[10];
in Super.h, It compiled.
However, I need the array that can be accessed in class A. So I have tried many ways, and found it can be compiled if I leave float array, and combine A.h and A.cpp, like
//Super.h
float *list=new float[10];
class Super{
public:
void function(){}
};
------------------------------------
// A.h
#pragma once
#include "Super.h"
class A{
public:
void function();
};
void A::function(){
std::cout<<"Working A!"<<std::endl;
}
Do I have to combine A.cpp and A.h? or is my design bad? I use XCode 8.
In your actual code, "
line" is declared in Settings.h and used in it'sdrawPixelfunction.The only other place that it's used is in main.cpp I'm going to discuss these points in reverse order so our discussion can make more sense:
delete[] pixelsYou should be using avector<float>instead so you avoid all the problems associated with dynamic memory:drawPixelis called after cleanup?mainfunction doesn't exit early and miss this?glDrawPixels(width, height, GL_RGB, GL_FLOAT, pixels)The finalglDrawPixelsparameter is of typevoid*So, if you've correctly replacedpixelswith avector<float>you can make the implicit conversion by passingdata(pixels)fill_n(pixels, width*height * 3, 1.0f)Rather than in Settings.h, this is wherepixelsshould be defined:vector<float> pixels(width * height * 3, 1.0F)you'll need to make these changes to your system to avoid the global variable:void draw()will need to be changed tovoid draw(vector<float>& pixels)and when anydrawmethod is calledpixelswill be the argumentvoid drawPixel(const int& i, const int& j, const float& red, const float& green, const float& blue)should be changed tovoid drawPixel(vector<float>& pixels, const int i, const int j, const float red, const float green, const float blue)and any call to thedrawPixelfunction must takepixelas it's 1st argumentvoid drawLine(const int& i0, const int& j0, const int& i1, const int& j1, const float& red, const float& green, const float& blue)should be changed tovoid drawLine(vector<float>& pixels, const int i0, const int j0, const int i1, const int j1, const float red, const float green, const float blue)and any call to thedrawLinefunction must takepixelas it's 1st argument