Error: cannot convert 'ship' to 'ship*' in assignment - c++

So I am working on a radar. I made a version for only one ship and everything worked fine. For some reason this version does not work.
Compiler:
Error: cannot convert 'ship' to 'ship*' in assignment
Code:
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>
using namespace std;
struct ship{
char name;
int x;
int y;
};
ship setPosition(ship space_ships[], int amount);
void update(ship space_ships[], char radar[][20], int x, int y);
int main () {
srand( time( NULL ) );
ship space_ships[2];
int x = 10;
int y = 20;
char radar[10][20];
update(space_ships, radar, x, y);
return 0;
}
ship setPosition(ship space_ships[], int amount){
for (int i=0; i<amount; i++){
int liczba = rand()%10;
int znak = rand()%26 + 65;
space_ships[i].x = liczba;
space_ships[i].y = 2*liczba;
space_ships[i].name = znak;
return space_ships[i];
}
}
void update(ship space_ships[], char radar[][20], int x, int y){
space_ships = setPosition (space_ships, 2);
Sleep(2000);
system("cls");
update(space_ships, radar, x, y);
}
Any ideas?

space_ships = setPosition (space_ships, 2);
This happens because your function setPosition returns value type "ship". And then you are trying to assign this to array (space_ships).

SetPosition doesn't do what you think it does. In your case it changes position of first ship in array and exits, returning that ship's object copy.

Related

Calculate car movement and distance with C++ classes

This program compiles however, I need to get this function to move on the x & y coordinate and then output the total distance traveled. The xCord moves it right and left while the yCord moves it up and down. I think I need to update my int Taxicab::getDistanceTraveled(), void Taxicab::moveX(int getX), & void Taxicab::moveX(int getX). But for the life of me can't figure out what to do to get it to update properly. When I compile and run it gives me 132617596 for cab1 distance travelled and 0 for the Y coordinate on cab2. Thanks for the help!
#ifndef TAXI_CPP
#define TAXI_CPP
class Taxicab
{
private:
int xCord;
int yCord;
int totalDist;
public:
Taxicab(); //default constructor
Taxicab(int, int); //overload constructor
int getX(); //returns X coordinate
int getY(); //returns Y coordinate
int getDistanceTraveled(); //returns distance calculation
void moveX(int); //moves X left or right
void moveY(int); //moves Y left or right
};
#endif // !TAXI_CPP
#include "Taxi.h"
#include <iostream>
#include <math.h>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
using std::abs;
Taxicab::Taxicab() //default constructor
{
}
Taxicab::Taxicab(int xCord, int yCord) //overload constructor
{
xCord = 0; //initialize to 0
yCord = 0; //initialize to 0
totalDist = 0; //initialize to 0
}
int Taxicab::getX()
{
return xCord; //return x coordinate
}
int Taxicab::getY()
{
return yCord; //return y coordinate
}
void Taxicab::moveX(int getX)
{
int moveX = 0;
moveX = moveX + getX;
}
void Taxicab::moveY(int getY)
{
int moveY = 0;
moveY = moveY + getY;
}
int Taxicab::getDistanceTraveled()
{
return abs(xCord) + abs(yCord);
}
#include <iostream>
#include "Taxi.h"
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
Taxicab cab1;
Taxicab cab2(5, -8);
cab1.moveX(3);
cab1.moveY(-4);
cab1.moveX(-1);
cout << cab1.getDistanceTraveled() << endl;
cab2.moveY(7);
cout << cab2.getY() << endl;
}
Your constructors do not make sense.
In default constructor you have to initialize member variables to something, otherwise their values are undefined and could be set to some random value. Try these maybe:
Taxicab::Taxicab() //default constructor
{
xCord = 0; //initialize to 0
yCord = 0; //initialize to 0
totalDist = 0; //initialize to 0
}
Taxicab::Taxicab(int xCord, int yCord) //overload constructor
{
this->xCord = xCord;
this->yCord = yCord;
totalDist = 0; //initialize to 0
}
Methods to move taxi also do not make much sense. Maybe something like that would be better:
void Taxicab::moveX(int offsetX)
{
totalDist += abs(offsetX);
xCoord += offsetX;
}
void Taxicab::moveY(int offsetY)
{
totalDist += abs(offsetY);
yCoord += offsetY;
}
int Taxicab::getDistanceTraveled()
{
return totalDist;
}

For-Loop Segmentation -- Excessive run ( i < SIZE, but i = SIZE)

This is a function in a program replicating Sierpinski's gasket. This function is supposed to attach the points in the triangle for the fractal.
After much deliberation I've figured out where the issue lies:
void add_pts(int &x, int &y)
{
Vector_ref<Rectangle> pt;
for (int i = 0; i < POINTS; ++i)
{
pt_test; //generates changing x, y vals within the limits of the triangle
cout << "pass" << i <<endl;
pt.push_back(new Rectangle(Point(x,y),5,5));
pt[i].set_fill_color(Color::yellow);
win.attach(pt[i]);
}
}
The output is "pass1...pass[POINTS-1]", but for whatever reason it runs when i = POINTS and runs into the segmentation error. I have no clue as to why. Can anyone assist, please?
Here is my code. The pt_test and coord are a bit sloppy but seeing as it can't run properly it's very hard to ascertain what I can streamline.
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <time.h>
#include "Simple_window.h"
#include "Graph.h"
#include "Point.h"
#include "GUI.h"
#include "Window.h"
using namespace std;
using namespace Graph_lib;
// globals
const int POINTS = 5000;
unsigned int seed = (unsigned int)time(0);
Simple_window win(Point(100,100),1100,700,"Homework 9");
// function declarations
double random(unsigned int &seed);
bool coords(int &x, int &y);
void pt_test(int x, int y);
void add_pts(int &x, int &y);
int main()
{
int x, y;
// title
Text title(Point(400,50), "The Sierpinski Gasket");
title.set_font(Graph_lib::Font::helvetica_bold);
title.set_font_size(25);
title.set_color(Color::cyan);
win.attach(title);
// triangle
Closed_polyline tri;
tri.add(Point(250,75)); // A
tri.add(Point(850,75)); // B
tri.add(Point(550,675)); // C
tri.set_fill_color(Color::white);
tri.set_color(Color::dark_red);
tri.set_style(Line_style(Line_style::solid,3));
win.attach(tri);
// vertices
Text vert_a(Point(225,70), "A (250, 75)");
vert_a.set_font(Graph_lib::Font::helvetica_bold);
vert_a.set_font_size(15);
vert_a.set_color(Color::cyan);
Text vert_b(Point(855,70), "B (850, 75)");
vert_b.set_font(Graph_lib::Font::helvetica_bold);
vert_b.set_font_size(15);
vert_b.set_color(Color::cyan);
Text vert_c(Point(575,670), "C (550, 675)");
vert_c.set_font(Graph_lib::Font::helvetica_bold);
vert_c.set_font_size(15);
vert_c.set_color(Color::cyan);
win.attach(vert_a);
win.attach(vert_b);
win.attach(vert_c);
// point selection
add_pts(x, y);
// window title and display
win.wait_for_button();
}
double random(unsigned int &seed)
{
const int MODULUS = 15749;
const int MULTIPLIER = 69069;
const int INCREMENT = 1;
seed = ((MULTIPLIER*seed)+INCREMENT)%MODULUS;
return double(seed)/double(MODULUS);
}
bool coords(int &x, int &y) // generates the points
{
x = int(251 + 600*random(seed));
y = int(76 + 600*random(seed));
if( y > (2*x-425) && x<= 550 || x>=550 && y < (-2*x + 1775))
return true;
}
void pt_test(int x, int y) // tests the points until they are within the range
{
coords;
while(coords == 0)
coords;
}
void add_pts(int &x, int &y) // attaches the points as shapes
{
Vector_ref<Rectangle> pt;
for (int i = 0; i < POINTS; ++i)
{
pt_test;
cout << "i == " << i << " points == " << POINTS << endl;
pt.push_back(new Rectangle(Point(x,y),5,5));
pt[i].set_fill_color(Color::yellow);
win.attach(pt[i]);
}
}
I've also noticed that the function add_pts doesn't work when the body is in the loop, but if you put the body in int_main(), it runs indefinitely but doesn't reach the segmentation fault as quickly, if at all.

undefined reference to function in main()

i tried to make a program on seperate files. Unfortunatelty i had erros while trying to build the code. It was pointing on undefined references to constuctors,destructos and function CzynnikiPierwsze. So i decied to put the whole code in one code. Still there is a problem in main() function: undefined reference to 'CzynnikiPierwsze(int)' Any ideas whats wrong? Here is the whole code:
#include <iostream>
#include <cctype>
#include <vector>
using namespace std;
vector<int> CzynnikiPierwsze(int);
class NieprawidlowaDana //wyjatki
{};
class SpozaZakresu
{};
class RozkladLiczby{
private:
int *tab;
public:
RozkladLiczby(int); //konstruktor
vector<int> CzynnikiPierwsze(int); //metoda
~RozkladLiczby();
};
/////////////////BODY of the CLASS/////////////////////////////////////
RozkladLiczby::~RozkladLiczby() //destruktor
{}
RozkladLiczby::RozkladLiczby(int n){
int* tab = new int[n+1];
int i,j;
for( i=0;i<=n;i++)
tab[i]=0; //zerujemy tablice
for( i=2;i<=n;i+=2)
tab[i]=2; //zajmujemy sie liczbami parzystymi
for(i=3; i<=n;i+=2)
for(j=i;j<=n;j+=i) //sito erastotesa
if(tab[j]==0)
tab[j]=i;
}
vector<int> RozkladLiczby::CzynnikiPierwsze(int m){
vector<int> tablica;
while(m!=1){
tablica.push_back(tab[m]);
m=m/tab[m];
}
return tablica;
}
////////////////////////END OF THE BODY//////////////////////////////
int parsuj(char* argz){
int i=0;
while(argz[i] != '\0'){ //funckja ktora konwertuje na int i sprawdza czy wprowadzaony zostal string
if( !isdigit(argz[i]))
throw NieprawidlowaDana();
i=i+1;
}
int x = stoi(argz);
if (x >= 2)
return x;
else
throw SpozaZakresu();
}
//////////////////GLOWNY BLOK///////////////////////////////////////
int main(int argc,char* argv[]){
vector<int> tablica,p;
int i,n;
int max;
for( i=1;i<=argc-1;i++){
n = parsuj(argv[i]);
tablica.push_back(n);
}
max=tablica[0];
for(i=1; i<=argc-1;i++){
if(tablica[i]>max)
max=tablica[i]; } // sprawdzamy max
RozkladLiczby odp = RozkladLiczby(max); //utwoorzylismy obiekt z maxa
for(unsigned int i=0;i<=tablica.size()-1;i++){
p=CzynnikiPierwsze(tablica[i]);
cout<<tablica[i]<<" = ";
int x= p[0];
int licznik = 1;
for(unsigned int j=1;j<=p.size()-1;j++){
if(x==p[j])
licznik++;
else if(licznik!=1)
cout<<x<<"^"<<licznik<<"*";
else
cout<<x<<"*";
}
cout<<endl;
}
return 0;
}
I would be grateful if u could solve this.
You have declared global function vector<int> CzynnikiPierwsze(int); but you have not defined it anywhere in your program. In your main you are calling global function and not the one which is your class member.

Passing Pointers to Classes in C++

I am trying to pass a pointer into my classes function, have it incremented, and have the variable retain it's value using pointers. Heres my code, it doesnt increment.
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int y;
};
test::test(int * currentY):
y(*currentY)
{
}
int test::addTo()
{
y++;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
int * number = &pointedAt;
test t(number);
t.addTo();
cout <<*number;
char f;
cin >>f;
}
}
This should do it:
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int *y;
};
test::test(int *currentY):
y(currentY)
{}
int test::addTo()
{
++*y;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
test t(&pointedAt);
t.addTo();
cout << pointedAt;
}
}
You have to store a pointer to the integer, so it refers to the same address as the original variable.

Compiler says that pointer is "out of scope". What basic thing am I doing wrong?

Okay, I've been running into some inconsistency and it's driving me towards a fist-sized hole in my moniter I'd rather avoid.
I'm going through an SDL tutorial on www.sdltutorials.com (the sdl-tutorial-basics tutorial I'm sure more than a few people have gone through by Tim Jones) and I ran into an error: ‘Surf_Display’ was not declared in this scope.
So, trying to figure out what's going wrong I wrote similar pointer code into an old rectangle program I used to refresh myself with the basics of C++ classes, saw the same error with an int pointer, and then tried to isolate the issue in something more specific.
Well, the more specific program compiles fine while the others explode so I'm guessing it's something pretty basic I'm missing, but GISing "C++ pointer class", etc, does nothing and I don't know how to get more specific.
Anyway...some code.
The program that works...
ct.h
#ifndef _CT_H_
#define _CT_H_
class Classtest2
{
private:
int *p_x;
public:
Classtest2();
void set_x(int);
int get_x();
};
#endif
ct.cpp
#include "ct.h"
#include <cstddef>
Classtest2::Classtest2()
{
p_x = 0;
}
void Classtest2::set_x(int x)
{
//do something neat here
}
int Classtest2::get_x()
{
return *p_x;
}
classtest2.cpp
#include "ct.h"
int main (int argc, char *argv[])
{
Classtest2 ct2;
return 0;
}
compiled with
g++ classtest2.cpp ct.h ct.cpp -o bin/classtest2
Now...the program that doesn't...
The classic...
#ifndef _RECTANGLE_H
#define _RECTANGLE_H
namespace shapes
{
class rectangle
{
public:
int height;
int width;
int *weight;
rectangle (int, int);
rectangle ();
int area ();
void setHeight(int);
void setWidth(int);
void setDimensions(int, int);
};
}
#endif
The classic cpp with weight...
#include "Rectangle.h"
#include <cstddef>
using namespace shapes;
rectangle::rectangle(int h, int w)
{
height = h;
width = w;
weight = NULL;
}
rectangle::rectangle()
{
height = 0;
width = 0;
weight = NULL;
}
int rectangle::area ()
{
return height * width;
}
void rectangle::setHeight(int h)
{
height = h;
}
void rectangle::setWidth(int w)
{
width = w;
}
void rectangle::setDimensions(int h, int w)
{
height = h;
width = w;
}
And the classic "do something simple" main...
#include "Rectangle.h"
#include <iostream>
using namespace std;
using namespace shapes;
int main(int argc, char* argv[])
{
rectangle tr(5,8);
cout << tr.height << endl;
cout << tr.width << endl;
return 0;
}
The first program compiles fine, but of course it doesn't actually do anything so that's not too surprizing. The second one (the rectangle program that also doesn't actually do anything) doesn't compile. I get the following...
g++ classtest1.cpp Rectangle.h Rectangle.cpp -o bin/classtest1
Rectangle.cpp: In constructor ‘shapes::rectangle::rectangle(int, int)’:
Rectangle.cpp:10: error: ‘weight’ was not declared in this scope
Rectangle.cpp: In constructor ‘shapes::rectangle::rectangle()’:
Rectangle.cpp:17: error: ‘weight’ was not declared in this scope
So, why can the first program "see" int *p_x while the second cannot see int *weight? I've been trying to figure out what I'm doing different and getting nowhere.
The right syntax for Rectangle.cpp is this:
#include "Rectangle.h"
#include <cstddef>
namespace shapes {
rectangle::rectangle(int h, int w)
{
height = h;
width = w;
weight = NULL;
}
rectangle::rectangle()
{
height = 0;
width = 0;
weight = NULL;
}
int rectangle::area ()
{
return height * width;
}
void rectangle::setHeight(int h)
{
height = h;
}
void rectangle::setWidth(int w)
{
width = w;
}
void rectangle::setDimensions(int h, int w)
{
height = h;
width = w;
}
} // namespace shapes
My GCC-4.4.5 and MSVC process your using namespace variant OK too. But it is not correct according to the current standard (you specify ::rectangle::area(), etc. instead of ::shapes::rectangle::area(), etc.).
You don't have errors in the code you've shown. Maybe you have a typo in int *wight declaration in Rectangle.h on your machine, but you've posted the correct code here.