As a beginner programer, I can't seem to catch what is going astray here. I removed some code to show that the error results from this simple syntax:
main:
#include <iostream>
#include <vector>
using namespace std;
class Grader
{
public:
Grader( );
void addScore( int score )
{
vectorofints.push_back(score);
}
vector<int> vectorofints;
};
int main()
{
Grader g;
return 0;
}
You've declared a constructor for Grader so must provide an implementation. The easiest way to do this is to change
Grader( );
to
Grader( ) {}
Alternatively, if you just removed the declaration of the constructor, the compiler will generate it for you.
Related
I've started to "port" my Java implementation of dynamic array to C++ and somehow i'm facing a strange linker error (LNK2019 and LNK2011) in my code, although i don't see any place which can produce such error.
I think it has something to do with constructor, but i can't find what exactly.
Thank you for your help!
DynStackArrayQueue.cpp (test class) :
#include <iostream>
#include "DynArray.h"
using namespace std;
int main()
{
DynArray dynA (3, 6);
}
DynArray.h
#pragma once
class DynArray {
private:
int growthFactor, maxOverhead;
int elements[];
public:
DynArray(int growthFactor, int maxOverhead);
}
DynArray.cpp
#include <iostream>
#include "DynArray.h"
using namespace std;
DynArray::DynArray(int growthFactor, int maxOverhead) {
this->growthFactor = growthFactor;
this->maxOverhead = maxOverhead;
}
The error itself:
You have missed a semicolon at the end of the class declaration. This leads to a compilation error, which you actually should be able to see before the linker error. Solution: add a semicolon after the closing brace of your DynArray class definition.
I'm writing my code on linux . But g++ always tells me"Use of undeclared identifier 'random'".I don't know why I have declare it in "Myvector.h"
my code is like :
Myvector.h
class MyVector {
private:
std::vector<double> data;
const int N;
static bool _bDim;
public:
MyVector(); //默认初始化
MyVector(int a); //设置维度初始化
MyVector(std::initializer_list<double> list);
~MyVector();
double &operator[](int);
MyVector &operator=(const MyVector a) {
MyVector b(outN(a));
this->data = a.data;
return *this;
};
friend MyVector random(int a);
}
#endif // MYVECTOR_H_
Myvector.cpp
#include "Myvector.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
bool MyVector::_bDim = true;
MyVector::MyVector() : N(3) {
data = vector<double>(N, 0.0);
_bDim = false;
};
MyVector::MyVector(int a) : N(a) {
data = vector<double>(N, 0.0);
_bDim = false;
};
MyVector::MyVector(std::initializer_list<double> list) : N(list.size()) {
for (auto i = list.begin(); i != list.end(); i++) {
data.push_back(*i);
}
};
MyVector::~MyVector(){
};
double &MyVector::operator[](int i) { return data[i]; }
MyVector random(int a){
MyVector u(a);
srand(time(NULL));
for(int i=0;i<a;i++){
u[i]=rand();
}
return u;
}
main.cpp
#include "Myvector.h"
#include <iostream>
#include<cstdlib>
#include<math.h>
#include <time.h>
using namespace std;
int main(){
MyVector z=random(1);
return 0;}
In fact ,I just know nothing about it. Is there someone going to help me?Thank you.
Below is nothing meaningful. I just need more words to ask this problem.
In the main function of the main.cpp file the following function is called:
MyVector z=random(1);
This appears to be a function which takes a single int argument. Additionally, there is such a function defined in the Myvector.cpp but not declared in Myvector.h (i.e., the main.cpp file does not see any function declaration for the definition).
Update the Myvector.h header to declare the MyVector random(int a) function. Also, the friend declaration is for a random function with 2 parameters, which doesn't look right.
You have to declare the function random somewhere, e.g.
#include "Myvector.h"
#include <iostream>
#include<cstdlib>
#include<math.h>
#include <time.h>
using namespace std;
MyVector random(int);
int main(){
MyVector z=random(1);
return 0;
}
The problem is random in your main function. That one is not declared.
Declaring a friend function means that function has access to the class as if it were a method. It doesn't declare the function at any time, just allows it inside the class.
Your random function is defined is some header file you have included. In your error message you see the return type of random is long int. And you have declared it as MyVector. I am not sure if math.h or time.h have it.
Solution 1: Change the name of your function.
Solution 2: Put your function in a namespace in order to avoid name ambiguity.
Im having trouble with a multi-file setup. Im working in visual studio, and, for whatever reason, my friend function in my class is not being defined in main. Any help would be appreciated, thanks.
BullCow.h:
#pragma once
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
class BullCow {
public:
BullCow();
friend int getWins();
static int Wins;
private:
int Attempts;
};
BullCow.cpp:
#include "stdafx.h"
#include "BullCow.h"
int BullCow::Wins = 0;
int getWins() {
return Wins;
}
BullCowMain.cpp:
#include "stdafx.h"
#include "BullCow.h"
int main()
{
srand(time(NULL));
std::cout << getWins();
return 0;
}
Note: It's an incomplete program, so some code (srand) is not used yet. I just included everything to better help figure out what's wrong.
getWins() needs at least a declaration in the .h file.
Since it's a friend, getWins() is not a member of the class, so it must be declared either directly in BullCowMain.cpp or in some file BullCowMain.cpp includes.
Add this somewhere outside of the class in your header:
int getWins();
Also, inside getwins, the return should be:
return BullCow::Wins;
Thanks #user4581301!
I am learning about smart pointers and when trying to compile the following "stupid" code I get an error.
#include <memory>
#include <iostream>
class Test
{
std::string myString="dumm";
};
int main()
{
std::unique_ptr<Test> test(new Test());
std::cout<<test->myString<<std::endl;
return 0;
}
I just wanted to see, whether this works but I get :"Applying -> to std::unique_ptr instead of a pointer", which seems weird.
I am using c++ 11
Eit: The error is now fixed and I cancompile the above code. However, CLion still gives me "Cant apply -> to std::uniq_ptr"-stuff, which seems to be an error with the IDE
In a class the default visibility is private which makes the myString field invisible to the test object. Make it public:
#include <iostream>
#include <memory>
#include <string>
class Test {
public:
std::string myString = "dumm";
};
int main() {
std::unique_ptr<Test> test(new Test());
std::cout << test->myString;
}
Prefer std::make_unique to direct use of new if compiling for C++14 and later:
std::unique_ptr<Test> test = std::make_unique<Test>();
This function is not available in the C++11 standard which is what you are using.
I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.