I am new to c++ . The program doesn't compile i am using Xcode. It doesn't give me any error. It just doesn't compile which's weird. Thanks for the help!
I am using 3 files as you can see. I think. there's a problem with the implementation that i can not figure out, if you have any suggestion for a better IDE that Xcode than would be helpful.
Thanks again
Car.cpp
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
void Car:: setMaker(string m)
{
maker=m;
}
string Car:: getMaker()
{
return maker;
}
void Car:: setModel(int m)
{
model=m;
}
int Car:: getModel()
{
return model;
}
Car.h
#ifndef _CAR_H;
#define _CAR_H;
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Car
{
private:
string maker;
int model;
public:
void setMaker(string m);
string getMaker();
void setModel(int m);
int getModel();
};
#endif
main.cpp
#include <iostream>
#include "Car.cpp"
#include "Car.h"
using namespace std;
int main ()
{
Car c1;
c1.setMaker("BMW");
c1.getMaker();
return 0;
}
Related
Relearning C/C++ after 3 years of JavaScript (I've gotten way too comfortable..)
I'm building a test file with input.
The problem is within cTool, where the first function is not letting me return a string. I thought this was totally valid if the library is included in the header file? What am I overlooking here.
cTool.cpp
string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
You need to add #include "cTool.h" to cTool.cpp, not just to cMain.cpp only. Otherwise, when compiling cTool.cpp, the compiler doesn't know what a string is since it doesn't see your #include <string> and using namespace std; statements (BTW, using namespace std; in a header file is a very bad idea).
cTool.cpp
#include "cTool.h" // <-- ADD THIS!
std::string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <iostream>
std::string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
I'm developing a simulation game in cpp using Visual Studio 2017 for School and in the development phase I got stuck in this situation.
So, what I did was create a new project to try and recreate that issue in the simplest form, so that it would be easier to debug.
Below is the main file and all the associated source codes:
main.cpp
#include "header.h"
#include "Vehicle.h"
#include "Car.h"
int main() {
Vehicle v;
v.addCar(1);
v.addCar(2);
v.addCar(3);
cout << v.getCars()[1].id << endl;
v.getCars()[1].id = 99;
cout << v.getCars()[1].id << endl;
system("pause");
return 0;
}
header.h
#ifndef CLUSTE2R_H
#define CLUSTE2R_H
#pragma once
#include <iostream>
#include <vector>
using namespace std;
#endif
Car.h
#ifndef CLUSTE1R_H
#define CLUSTE1R_H
#pragma once
#include "Vehicle.h"
using namespace std;
class Car : public Vehicle
{
public:
int id;
Car(int id);
~Car();
};
#endif
Car.cpp
#include "Car.h"
Car::Car(int id)
{
this->id = id;
}
Car::~Car()
{
}
Vehicle.h
#ifndef CLUSTER_H
#define CLUSTER_H
#pragma once
#include <vector>
//#include "Car.h"
class Car;
using namespace std;
class Vehicle
{
private:
vector<Car> cars;
public:
Vehicle();
~Vehicle();
vector<Car> getCars();
void addCar(int id);
};
#endif
Vehicle.cpp
#include "Vehicle.h"
#include "Car.h"
#include <vector>
using namespace std;
//class Car;
Vehicle::Vehicle()
{
}
Vehicle::~Vehicle()
{
}
vector<Car> Vehicle::getCars()
{
return this->cars;
}
void Vehicle::addCar(int id)
{
Car c(id);
cars.reserve(cars.size() + 1);
cars.push_back(c);
}
So, what I'm trying to do is to get the following output:
2 \n 99
This is what I'm getting:
2 \n 2
What am I doing wrong? I believe the issue is associated with the main.cpp file. But I'm not quite sure how to achieve what I want in any other way...
Currently, you are returning a new instance of a vector when you call getCars() function from your Vehicle, this means that all changes to the vector will not be applied to the original vector in the class.
To fix this you could just return a reference of the vector(changing the vector<Car> getCars(); to std::vector<Car>& getCars()).
You could also make a local copy of the vector and then setting the vector to the class.
I have a class Project and each Project can have different tasks.
Project.h:
#pragma once
#include "Task.h"
#include <vector>
using namespace std;
class Project
{
private:
vector<Task> Tasks;
public:
Project::Project(int inleesgetal);//constructor
vector<Task> GetTasks();
};
Project.cpp:
#include "Project.h"
#include <string>
#include <vector>
Project::Project(int inleesgetal)
{
//constructor
Tasks.resize(Numbertasks);
}
vector<Task> Project::GetTasks()
{
return Tasks;
}
Task.h:
#pragma once
#include <vector>
using namespace std;
class Task
{
private:
//Info:
int StartTime_Solo;
public:
Task(); //constructor
void SetStartTime_Solo(int st_s);
int GetStartTime_Solo();
};
Task.cpp:
#include "Task.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
Task::Task()
{
//constructor
StartTime_Solo = 0;
}
int Task::GetStartTime_Solo()
{
return StartTime_Solo;
}
void Task::SetStartTime_Solo(int st_s)
{
StartTime_Solo = st_s;
}
main:
#include <iostream>
#include <vector>
#include "Task.h"
#include "Project.h"
using namespace std;
int main()
{
Project Project1(6);
Project1.GetTasks()[2].SetStartTime_Solo(55);
cout << "test:" << Project1.GetTasks()[2].GetStartTime_Solo();
return 0;
}
Now when I try to set the 3rd task of Project1 to a starttime of 55 and then print the start time out it still gives me 0 as a result.
Why is this? And how can I change my code so it actually sets the starttime to 55?
vector<Task> GetTasks();
should be
const vector<Task>& GetTasks() const;
vector<Task>& GetTasks();
And so with definitions:
vector<Task> Project::GetTasks()
{
return Tasks;
}
should be:
const vector<Task>& Project::GetTasks() const { return Tasks; }
vector<Task>& Project::GetTasks() { return Tasks; }
The problem is that you are returning a copy of the vector<Task> from the GetTasks function. You then modify this copy and throw it away right afterwards. The internal member of Project is not changed.
If you return by reference like this:
vector<Task>& GetTasks();
Then you are basically returning something that points to the internal vector, and so when you modify it, you actually modify the member data of your class.
So I decided to start learning c++ I wanted to find out how to use classes in it. I thought I had set it up correctly (From looking at tutorials) but it gives me the following error..
C:\Dev-Cpp\main.cpp `Math' does not name a type
I am new to c++ and the Dev-C++ Compiler so I haven't figured out the errors yet. HEre is my code..
Main class:
#include <cstdlib>
#include <iostream>
using namespace std;
//variables
int integer;
Math math;
int main()
{
math = new Math();
integer = math.add(5,2);
cout << integer << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Here is my Math.cpp class:
#include <cstdlib>
#include <iostream>
#include "Math.h"
using namespace std;
public class Math {
public Math::Math() {
}
public int Math::add(int one, int two) {
return (one+two);
}
}
And the Math header file:
public class Math {
public:
public Math();
public int add(int one, int two) {one=0; two=0};
};
Any help would be appreciated, I have been trying to work this out for awhile now.
You are using a lot of Java-ish syntaxes. What you should have is this (untested):
//main.cpp
#include <cstdlib>
#include <iostream>
#include "Math.h"
using namespace std;
int main()
{
//No Math m = new Math();
//Either Math *m = new Math(); and delete m (later on) or below:
Math m; //avoid global variables when possible.
int integer = m.add(5,2); //its m not math.
cout << integer << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
For Math.h
class Math { //no `public`
public:
Math();
int add(int, int);
};
For Math.cpp
#include <cstdlib>
#include <iostream> //unnecessary includes..
#include "Math.h"
using namespace std;
Math::Math() {
}
int Math::add(int one, int two) {
return (one+two);
}
And compile the two cpp files (in case of gcc)
$ g++ main.cpp Math.cpp
$ ./a.out
It seems you are using Dev C++ IDE, in that case the IDE compiles and executes the program for you.
I'm new to c++ and I usually can pick through errors and figure out what's wrong but I'm stumped.
I'm getting an error saying "line|10|error: 'string' in class 'mine' does not name a type"
Here is mine.h:
#ifndef MINE_H
#define MINE_H
#include <iostream>
#include <string>
using namespace std;
class mine
{
public:
mine();
string getName();
};
#endif // MINE_H
Here is mine.cpp:
#include "mine.h"
#include <iostream>
#include <string>
using namespace std;
mine::mine()
{
//ctor
}
mine::string getName()
{
}
mine::string getName()
{
}
Should have been
string mine::getName()
{
}
It should be:
string mine::getName()
{
}