I want to store data by using class but it so I write a .h and .C file :
#ifndef MY_lepton_collection
#define MY_lepton_collection
#include<iostream>
#include<vector>
using namespace std;
class data_collection {
public:
data_collection(float, float, float,float );
~data_collection();
float pt;
float eta;
float phi;
float en;
private:
};
#endif
#include "data_collection.h"
Lepton_collection::Lepton_collection(float pt1, float eta1, float phi1, float energy1){pt= pt1;eta = eta1;phi = phi1;en=energy1;
}data_collection::~data_collection(){}
and use it in the main code as:
std::vector<data_collection*>*Selecteddata;
for(int i=0; i<100; i++){
Selecteddata->push_back( new data_collection(Pt[i],Eta[i] ,Phi[i], Energy[i]));
}
but when I get cout of it, there is an error:
invalid memory pointer passed to a callee: cout\<\<(\*Selecteddata)-\>pt;
I don't know what part of it is wrong.
Related
I'm trying to achieve polymorphism with my image manipulation program. I keep getting this error, I think it is caused from me defining scale twice in both the header file and the cpp file.
Error C2011 'scale': 'class' type redefinition
and I am not sure what to do. Thanks for any help.
.cpp file
Image *begin= new Image(750, 750);
begin->read("picture1.jpg");
Image *enlarged= new Image(1500, 1500);
scale *pass= new scale(*imgScale);
pass->manipulator(1500, 1500);
pass->load("manipulated.jpg");
class scale : public Image
{
public:
scale(Image const &firstimg)
{
w = firstimg.w;
h = firstimg.h;
pixels = firstimg.pixels;
}
void manipulator(int w2, int h2)
{
Image *temp = new Image(w2, h2);
float x_ratio = (float )w / w2;
float y_ratio = (float )h / h2;
float px, py;
for (int i = 0; i < h2; i++)
{
for (int j = 0; j < w2; j++)
{
px = floor(j*x_ratio);
py = floor(i*y_ratio);
temp->pixels[(i*w2) + j] = this->pixels[(int)((py*w) + px)];
}
}
}
};
header file
#pragma once
#ifndef manipulator_H
#define manipulator_H
class scale : public Image
{
public:
scale(Image const &firstimg);
void manipulator(int w2, int h2);
};
#endif
You are declaring you class Scale in two different files, in the algo header file and in the .cpp file. Actually, I don't know why are how using inheritance if you are creating a new Image in your zoom function.
Your header, scale.h should be something like that:
#pragma once
#ifndef ALGORITHMS_H
#define ALGORITHMS_H
class Image;
class Scale {
public:
explicit Scale(Image const &beginImg);
void zoom(int w2, int h2);
private:
// Here all your private variables
int w;
int h;
¿? pixels;
};
#endif
And your cpp file, scale.cpp:
#include "scale.h"
#include "image.h"
Scale::Scale(Image const &beginImg) :
w(beginImg.w),
h(beginImg.h),
pixels(beginImg.pixels) {}
void Scale::zoom(int w2, int h2){
Image *temp = new Image(w2, h2);
double x_ratio = (double)w / w2;
double y_ratio = (double)h / h2;
double px, py;
// rest of your code;
}
And, then in the place you want to use this class, example your main:
int main() {
Image *imgScale = new Image(750, 750);
imgScale->readPPM("Images/Zoom/zIMG_1.ppm");
Scale *test = new Scale(*imgScale);
test->zoom(1500, 1500);
test->writePPM("Scale_x2.ppm");
delete imgScale;
delete test;
return 0;
}
In any case, consider using smart pointers instead of raw pointers and take a look in to the different modifications I did.
This is for a programming assignment in one of my classes. I'm supposed to take a list of points and sort them based on the distance from a reference point. The prompt says to use a struct to store the x,y,z values of each point, and to use another struct to store the points and number of points. I get an error when i try to compile saying
Points.h:6:2: error: 'Point' does not name a type
Points.cpp: In function 'Points* readPoints(const char*)':
Points.cpp:25:11: error: 'struct Points' has no member named 'pointsarray'
What is causing this error, and how can i fix it?
There are four files concerning this, Points.h, Point.h, Points.cpp, Point.cpp.
Here's a copy and paste of Points.h:'
#if !defined POINTS
#define POINTS
struct Points
{
Point** pointsarray;
int num_points;
};
Points* readPoints(const char file_name[]);
void destroyPoints(Points* pointsarray);
void DisplayPoints(Points* pointsarray);
#endif
And here's a copy of Points.cpp:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include "Points.h"
#include "Point.h"
Points* readPoints(const char file_name[])
{
ifstream input_file;
input_file.open(file_name);
int ARRAY_SIZE = 0;
input_file >> ARRAY_SIZE;
int i = 0;
double x = 0;
double y = 0;
double z = 0;
Points* points;
points->num_points = ARRAY_SIZE;
for(i = 0; i < ARRAY_SIZE; i++){
input_file >> x;
input_file >> y;
input_file >> z;
Point* point = createPoint(x,y,z);
points->pointsarray[i] = point;
}
return points;
}
Here's Point.cpp:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
#include "Point.h"
#include "Points.h"
#if !defined NULL
#define NULL = 0
#endif
Point* createPoint(double x, double y, double z)
{
Point* point;
point.x = x;
point.y = y;
point.z = z;
return point;
}
void destroyPoint(Point* point)
{
delete point;
}
void displayPoint(Point* point)
{
cout << setprecision(3) << fixed << "(" << point->x << ", " << point->y << ", " << point->z << ")" << endl;
}
And here's point.h
#if !defined POINT
#define POINT
struct Point
{
double x;
double y;
double z;
};
Point* createPoint(double x, double y, double z);
void destroyPoint(Point* point);
void displayPoint(Point* point);
#endif
I would greatly appreciate any solutions you can give me, thanks in advance.
You have:
struct Points
{
Points** pointsarray;
int num_points;
};
Perhaps you mean to have:
struct Points
{
Point** pointsarray;
int num_points;
};
Otherwise, pointsarray is an array of Points* not an array of Point*. That's why the compiler does not like the statement:
points->pointsarray[i] = point;
The RHS side of that line is a Point*, not a Points*.
Any and all help is greatly appreciate. Thank you for taking the time out to review my issue.
I am currently receiving an errors
1>c:\users\fordn_000\documents\tcc_odu\it310\programs\it310_homework_program_2_nford\it310_homework_program_2_nford\Form1.h(625): error C2653: 'Marshal' : is not a class or namespace name
1>c:\users\fordn_000\documents\tcc_odu\it 310\programs\it310_homework_program_2_nford\it310_homework_program_2_nford\Form1.h(625): error C3861: 'StringToHGlobalAnsi': identifier not found
This is my GUI form code, I want to use the command marshal however, that appears where there error is taking place
private: System::Void DisplayButton_Click(System::Object^ sender, System::EventArgs^ e)
{
int InitProductID = 0;
char* InitDescription;
int InitManufID = 0;
double InitWholeSale = 0.0;
double InitMarkup = 0.0;
int InitQuanity = 0;
String^ TypeString;
//EXTRACT FROM INPUT TEXT BOX'S
InitProductID = Convert::ToInt32(ProductIDNumberBoxNew->Text);
InitDescription = (char*)(void*)Marshal::StringToHGlobalAnsi(DescriptionBox->Text);
InitManufID = Convert::ToInt32(ManufacturerBox->Text);
InitWholeSale = Convert::ToDouble(WholesalePriceBox->Text);
InitMarkup = Convert::ToDouble(MarkupBox->Text);
InitQuanity = Convert::ToInt32(QuantityBox->Text);
//CREATE INSTANCE OF CLASS
Inventory InventoryItem(InitProductID, InitDescription, InitManufID, InitWholeSale, InitMarkup, InitQuanity);
//DISPLAY TO OUTPUT TEXT BOXS
ProductIDNumberOutBox->Text = Convert::ToString(InventoryItem.GetProductID());
TypeString=gcnew String(InventoryItem.GetDescription());
ManufacturerOutBox->Text = Convert::ToString(InventoryItem.GetManufID());
//RETAIL PRICE OUTBOX
QuantityOutBox->Text= Convert::ToString(InventoryItem.GetQuanity());
}
This is my stdafx header file below
#pragma once
// TODO: reference additional headers your program requires here
#include "Inventory.h"
This is my stdafx cpp file below
#include "stdafx.h"
#include "Form1.h"
Finally this is my inventory header file
//SPECIFICATION FILE (INVENTORY.H)
#ifndef INVENTORY_H
#define INVENTORY_H
#include <iostream>
#include <iomanip>
using namespace std;
class Inventory
{
private:
int ProductID;
mutable char Description[25];
int ManufID;
double WholeSale;
double Markup;
int Quanity;
public:
//CONSTRUCTORS
Inventory( );
Inventory(int, char[], int, double, double, int);
//GET FUNCTIONS
int GetProductID( )const;
char* GetDescription( )const;
int GetManufID( )const;
double GetWholeSale( )const;
double GetMarkup( )const;
int GetQuanity( )const;
//DISPLAY FUNCTION
void Display( )const;
//RETURN FUNCTION
double RetailPrice( )const;
};
#endif
I think you need to reference this:
using namespace System::Runtime::InteropServices;
I know the thermometer problems have been done to death but I thought I would give this a shot.
I keep getting the error messages "use of undeclared identifier 'converterc'" and "use of undeclared identifier 'converterf'". Any ideas?
Spike
#include <iostream>
#include "converters.h"
using namespace std;
int main()
{
int degree;
int weehoo;
cout<<"\n\n\n\t\t Enter the temperature : ";
cin>>degree;
cout<<"\n\n\t\t If the temperature is in Celsius enter 0, if Farenheit enter 1 :";
cin>>weehoo;
if (weehoo==0)
{
cout<<"\n\n\t\tThe temperature in Farenheit is "<<converterc(degree,weehoo)<<endl;
}
else
{
cout<<"\n\n\t\tThe temperature in Celsius is "<<converterf(degree,weehoo)<<endl;
}
return 0;
}
#ifndef __again_converters_h
#define __again_converters_h
#endif
#pragma once
class Thermometer
{
private:
float degreeC; //celcius
float degreeF; //farenheit
public:
void setCelcius (float c) {degreeC=c;}
void setFarenheit (float f) {degreeF=f;}
float getCelcius (void){return degreeC;}
float getFarenheit (void){return degreeF;}
Thermometer (float degree=0,float f=0, float c=0,float outtemp=0);
float converterc(int degree,int weehoo);
float converterf(int degree,int weehoo);
};
converters.cpp file
#include "converters.h"
float Thermometer::converterf(int degree,int weehoo)
{
degreeC=((degree-32) * (.5556));
return degreeC ;
}
float Thermometer::converterc(int degree,int weehoo)
{
degreeF=((1.8)*degree)+32;
return degreeF;
}
converterc and converterf are member function of the class Thermometer but you're calling them without a Thermometer instance.
How about creating a Thermometer instance in your main?
Thermometer tm;
tm.converterc(degree, weehoo);
converterc and converterf are functions in your class. This means they are there to be called on an object being an instance of this class or class derived from this.
class Thermometer
{
private:
float degreeC; //celcius
float degreeF; //farenheit
//...
public:
float converterc(int degree,int weehoo);
float converterf(int degree,int weehoo);
};
int degree = 1;
int weehoo = 2;
Thermometer t; //initialize it properly if this is needed before calling functions
float f = t.converterc(degree,weehooo);
using these functions in the way you did this:
float f = converterc(degree,weehooo);
is possible as:
float f = Thermometer::converterc(degree,weehooo);
but then they have to be static what means they don't have this pointer and are common to whole class (still you can call them using an instance of class but it is not necessary):
class Thermometer
{
private:
float degreeC; //celcius
float degreeF; //farenheit
//...
public:
static float converterc(int degree,int weehoo);
static float converterf(int degree,int weehoo);
};
I have some question for coding C++ program with header file.
This is my header.h file :
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double get_area();
void resize(double factor);
private:
double width;
double length;
double area;
double factor;
};
And this is my Question1.cpp file which store all the methods:
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length)
{
width = width; //I have no idea how to use this.something as its in Java
length = length; //Problem probably occurs at here
}
double Rectangle::get_perimeter()
{
return ((width * 2) + (length * 2)) ;
}
double Rectangle::get_area()
{
return (width * length);
}
void Rectangle::resize(double factor)
{
width *= factor;
length *= factor;
}
private:
double width;
double length;
double area;
double factor;
};
And lastly, here is my main method.cpp :
#include <iostream>
#include "header.h";
using namespace std;
int main()
{
Rectangle rectangle1(2.5,7.0);
cout << rectangle1.get_perimeter();
cout << rectangle1.get_area();
system("PAUSE");
return 0;
}
However, when I try to run the program, the system told me that there was build errors and unresolved externals which I have no idea why is it so. Could somebody please help me fix it?
Thanks in advance.
Your implementations should not look like
class Rectangle
{
public:
Rectangle(double width, double length) { .... }
but like
Rectangle::Rectangle(double width, double length) : width(width), length(length) {}
You need to include header.h in the implementation .cpp file and in any file that needs the definition of the Rectangle class. You also need include guards in your headers. And do not put using namespace std in a header. In fact, don't put it anywhere.
Change .h to ->
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double get_area();
void resize(double factor);
private:
double width;
double length;
double area;
double factor;
};
Then .cpp to->
#include <iostream>
#include "header.h"
using namespace std;
Rectangle::Rectangle(double width, double length)
{
this->width = width; //I have no idea how to use this.something as its in Java
this->length = length; //Problem probably occurs at here
}
double Rectangle::get_perimeter()
{
return ((this->width * 2) + (this->length * 2)) ;
}
double Rectangle::get_area()
{
return (this->width * this->length);
}
void Rectangle::resize(double factor)
{
this->width *= factor;
this->length *= factor;
}
This should work then.
Regards,
Luka
Few things to unpick here.
1) use this->width which is equivalent to java's this.width (In C++, this is a pointer). Actually some C++ programmers (including me) prefix member variables with m_. Then you can just write m_width = width.
2) include "header.h" at the top of Question1.cpp
3) avoid putting "using namespace std" in a header file, or you could get unintended namespace
clashes as your code expands. OK to put it in the separate source files though although some folk even discourage this.
4) depending on your compiler and linker, you'll need to link to various lib's that the iostream library uses. Perhaps if you tell us the compiler you're using, we can help you here.
5) you need to surround your header with
#ifndef ARBITRARY_TEXT_BUT_DISTINCT_FROM_ANY_OTHER_IN_YOUR_PROGRAM
#define ARBITRARY_TEXT_BUT_DISTINCT_FROM_ANY_OTHER_IN_YOUR_PROGRAM
...your code here
#endif
This is an include guard - helps prevent multiple inclusion of a header file contents.
In Question1.cpp you have to include header.h
And don't forget include guards in header.h
Also in Question1.cpp you must change
Rectangle(double width, double length)
to
Rectangle::Rectangle(double width, double length)
You need to tell your build system to compile "question1.cpp". Usually there is an "add existing file to project" menu item somewhere under "File".
And typically, your class and constructor would use a different name than the input parameter. Many people put a prefix at the begginning (mLength, iLength) or postfix at the end (length_ is common).
The other solution is to prefix the member variable with this->length, but that can get pretty messy after a while.
the big mistake that in the .cpp file you are supposed to implement only the methods not rewrite full class implementation try the following in .cpp file
Rectangle::Rectangle(double width, double length)
{
width = width; //I have no idea how to use this.something as its in Java
length = length; //Problem probably occurs at here
}
and don't include the methods in class{}; block and don't redefine your member variables
also don't forget to include the header file in the .cpp file
thanks
I wonder if you are getting linker errors as your cpp file is little weird
In the cpp file include the .h file and only implement the functions like
Rectangle::Rectangle(double width, double length){
//implement
}
double Rectangle::get_perimeter(){
//implement
}
double Rectangle::get_area(){
//implement
}
void Rectangle::resize(double factor){
//implement
}
When you want to split your class file into a *.cpp and a *.h file it always has the following form:
rectangle.h:
#ifndef __RECTANGLE_H_
#define __RECTANGLE_H_
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double width, double length);
double get_perimeter();
double get_area();
void resize(double factor);
private:
double width;
double length;
double area;
double factor;
};
#endif
now the rectangle.cpp should have the following form:
#include <iostream>
#include "rectangle.h"
using namespace std;
Rectangle(double width, double length)
{
width = width; //I have no idea how to use this.something as its in Java
length = length; //Problem probably occurs at here
}
double Rectangle::get_perimeter()
{
return ((width * 2) + (length * 2)) ;
}
double Rectangle::get_area()
{
return (width * length);
}
void Rectangle::resize(double factor)
{
width *= factor;
length *= factor;
}
so when as an explanation:
The header file tells you which fields and methods are available and the *.cpp file implements the methods.
In your main.cpp you just need o include the rectangle.h
this code below works
#include<iostream>
#include<iomanip>
using namespace std;
class student
{
private:
int area;//hours;
float perimeter;//gpa;
public:
void addcourse(int len, float wid)
{
float sum;
sum = area * perimeter;
area += len;
sum += wid * len;
perimeter = sum / area;
}
student() // constructor
{
area = 0;
perimeter= 0.0;
}
};
int main()
{
student s;
int length;//classhours;//l
float width;//classgpa;//w
cout << "Enter length ( 1 to end): ";
cin >> length;
while(length != 1)
{
cout << "Enter width: ";
cin >> width;
s.addcourse(length, width);
cout << "Enter length ( 1 to end): ";
cin >> length;
}
// cout << "Rectangle's length = " << r.getlength() << endl;
// cout << "Rectangle's width = " << r.getwidth() << endl;
// cout << "Rectangle's area = " << r.getarea() << endl;
// cout << "Rectangle's perimeter = " << r.getperimeter() << endl;
}