C++ Compiling Issues - c++

When compiled using g++ MainStudent.cpp Student.cpp
These are the errors i get :
MainStudent.cpp: In function ‘int main()’: MainStudent.cpp:23:38:
error: no matching function for call to ‘Student::Student(char [10],
char [10], int&, double [3])’ MainStudent.cpp:23:38: note: candidates
are: Student.h:13:2: note: Student::Student(char*, char*, int, double)
Student.h:13:2: note: no known conversion for argument 4 from
‘double [3]’ to ‘double’ Student.h:5:7: note: Student::Student(const
Student&) Student.h:5:7: note: candidate expects 1 argument, 4
provided Student.cpp: In constructor ‘Student::Student(char*, char*,
int, double)’: Student.cpp:9:11: error: incompatible types in
assignment of ‘double’ to ‘double [3]’ Student.cpp: At global scope:
Student.cpp:14:5: error: prototype for ‘int Student::Getage()’ does
not match any in class ‘Student’ Student.h:16:7: error: candidate is:
int* Student::Getage() Student.cpp:15:8: error: prototype for ‘double
Student::Getmarks()’ does not match any in class ‘Student’
Student.h:17:10: error: candidate is: double* Student::Getmarks()
I can't figure out where the problem lies...

Your constructor is
Student::Student (char *fname, char *lname, int age, double marks)
^^^^^^^^^^^^
But you are trying to pass an array to it in
double marks[3];
//...
Student st1(fname, lname, age, marks);
You either need to get rid of the array in the class and just take a double or change the constructor to take a double array and then copy it in the constructor like
Student::Student (char *fname, char *lname, int age, const double (&marks)[3]) {
// ^^^^^^^^^^^^^^^ use array of size 3
// since that is what _marks is
strcpy(_fname, fname);
strcpy(_lname, lname);
_age = age;
for (int i = 0; i < 3; i++)
_marks[i] = marks[i];
}

Related

why does it throw an error when i compile it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In index.hpp I have created a class that has multiple data members like int age, std::string city; etc. i have defined a constructor outside the class. In program.cpp i have created an object called sam. when i try to compile it, it shows error. what's the reason?
program.cpp
#include<iostream>
#include "index.hpp"
int main(){
profile sam("Sam Drakkila", 30, "New York", "USA", "he/him");
std::cout<<sam.name;
}
index.hpp
#include<iostream>
#include<vector>
class profile{
public:
std::string name;
int age;
std::string city;
std::string country;
std::string pronouns;
std::vector<std::string> hobbies;
};
profile::profile(std::string new_name, int new_age,std::string
new_city,std::string new_country, std::string
new_pronouns = "they/them"){
name = new_name;
age = new_age;
city = new_city;
country = new_country;
pronouns = new_pronouns;
}
error message
In file included from program.cpp:2:0:
index.hpp:15:1: error: prototype for 'profile::profile(std::__cxx11::string, int, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)' does not match any in class 'profile'
profile::profile(std::string new_name, int new_age,std::string
^~~~~~~
index.hpp:4:7: error: candidates are: profile::profile(profile&&)
class profile{
^~~~~~~
index.hpp:4:7: error: profile::profile(const profile&)
index.hpp:4:7: error: profile::profile()
program.cpp: In function 'int main()':
program.cpp:5:62: error: no matching function for call to 'profile::profile(const char [13], int, const char [9], const char [4], const char [7])'
profile sam("Sam Drakkila", 30, "New York", "USA", "he/him");
^
In file included from program.cpp:2:0:
index.hpp:4:7: note: candidate: profile::profile()
class profile{
^~~~~~~
index.hpp:4:7: note: candidate expects 0 arguments, 5 provided
index.hpp:4:7: note: candidate: profile::profile(const profile&)
index.hpp:4:7: note: candidate expects 1 argument, 5 provided
index.hpp:4:7: note: candidate: profile::profile(profile&&)
index.hpp:4:7: note: candidate: profile::profile(const profile&)
index.hpp:4:7: note: candidate expects 1 argument, 5 provided
index.hpp:4:7: note: candidate: profile::profile(profile&&)
index.hpp:4:7: note: candidate expects 1 argument, 5 provided
.\index }
index.hpp:15:1: error: prototype for 'profile::profile(std::__cxx11::string, int, std::__cxx11::string, std::__cxx11::string, std::__cxx11::string)' does not match any in class 'profile'
profile::profile(std::string new_name, int new_age,std::string
^~~~~~~
index.hpp:4:7: error: candidates are: profile::profile(profile&&)
class profile{
^~~~~~~
index.hpp:4:7: error: profile::profile(const profile&)
index.hpp:4:7: error: profile::profile()
i have defined a constructor outside the class
Yes, but you have not declared it inside the class.
Also, you should not put code inside header files unless you are creating a template.
To fix your problem, add the declaration to the class:
class profile{
public:
profile(std::string new_name, int new_age, std::string new_city, std::string new_country, std::string new_pronouns);
std::string name;
int age;
std::string city;
std::string country;
std::string pronouns;
std::vector<std::string> hobbies;
};
You should also consider moving the code of the constructor to index.cpp, and renaming the files profile.hpp and profile.cpp for clarity.
You need to declare the constructor inside the class definition:
class profile{
public:
std::string name;
int age;
std::string city;
std::string country;
std::string pronouns;
std::vector<std::string> hobbies;
profile::profile(std::string new_name, int new_age,std::string
new_city,std::string new_country, std::string
new_pronouns = "they/them");
};
As a side note: you should prefer a member initializer list for the task done by the constructor.

No matching function call call to constructor in header file

I have seen similar questions asked and tried their solutions but the answers to them do not seem to work. I have the following code:
.h
#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;
struct DialogueNode;
struct DialogueOption {
string text;
DialogueNode *next_node;
int return_code;
DialogueOption(string t, int rc, DialogueNode * nn) : text{t},
return_code{rc}, next_node{nn} {}
};
struct DialogueNode {
string text;
vector <DialogueOption> dialogue_options;
DialogueNode();
DialogueNode(const string &);
};
struct DialogueTree {
DialogueTree() {}
void init();
void destroyTree();
int performDialogue();
private:
vector <DialogueNode*> dialogue_nodes;
};
.cpp
#include "dialogue_tree.h"
DialogueNode::DialogueNode(const string &t) : text{t} {}
void DialogueTree::init() {
string s = "Hello";
for(int i = 0; i < 5; i++) {
DialogueNode *node = new DialogueNode(s);
dialogue_nodes.push_back(node);
delete node;
}
}
void DialogueTree::destroyTree() {
}
int DialogueTree::performDialogue() {
return 0;
}
int main() {
return 0;
}
I get the error: error: no matching function for call to ‘DialogueNode:: DialogueNode(std::__cxx11::string&)’ DialogueNode *node = new DialogueNode(s);
EDIT additional notes on error
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode()
dialogue_tree.h:17:8: note: candidate expects 0 arguments, 1 provided
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(const DialogueNode&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const DialogueNode&’
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(DialogueNode&&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘DialogueNode&&’
Which makes no sense to me because I have the constructor defined to take a string as an argument.
You've declared your constructor as:
DialogueNode(const string);
But defined it as:
DialogueNode(const string &t);
Those two aren't the same; the former takes a const string while the latter takes a const string reference. You'll have to add the & to specify a reference argument:
DialogueNode(const string &);
it is because in the constructor you are specifying that the parameter will be a string of constant type and when creating an object you are passing a string. The type mismatch is the problem, either fix the constructor parameter to string or change when you are creating an object.

BOOST_STRONG_TYPEDEF, new expression, ambiguous default type conversion

I would think this should work:
#include "boost/serialization/strong_typedef.hpp"
BOOST_STRONG_TYPEDEF(int, StrongInt);
int main()
{
StrongInt len (100);
int *heapInts = new int [len];
}
gcc 4.8.2:
./StrongTypeTest.cpp: In function ‘int main()’:
./StrongTypeTest.cpp:8:30: error: ambiguous default type conversion from ‘StrongInt’
int *heapInts = new int [len];
^
./StrongTypeTest.cpp:8:30: error: candidate conversions include ‘StrongInt::operator const int&() const’ and ‘StrongInt::operator int&()’

c++ multithreading class methods

I have following problem.
vector<thread> vThreads;
list<Crob *> lRobs;
list<Crob *>::iterator i;
for(i = lRobs.begin(); i != lRobs.end(); i++)
{
vThreads.push_back(thread((*i)->findPath));
}
I want to pass the method findPath to a thread, but I just get a lot of errors...
> labrob.cpp: In function ‘int main(int, char**)’:
labrob.cpp:72:43: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’
labrob.cpp:72:43: note: candidates are:
In file included from labrob.cpp:14:0:
/usr/include/c++/4.7/thread:131:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (Crob::*)(); _Args = {}]
/usr/include/c++/4.7/thread:131:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (Crob::*&&)()’
/usr/include/c++/4.7/thread:126:5: note: std::thread::thread(std::thread&&)
/usr/include/c++/4.7/thread:126:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’
/usr/include/c++/4.7/thread:122:5: note: std::thread::thread()
/usr/include/c++/4.7/thread:122:5: note: candidate expects 0 arguments, 1 provided
make: *** [labrob.o] Error 1
I have already tried to pass local functions and that worked without problems...
Added CRob header
#pragma once
#include "point.hpp"
#include "lab.hpp"
class Crob
{
protected:
Cpoint *pos;
int steps;
Clab *labfind;
string direction;
public:
Crob(Clab *lab);
virtual ~Crob();
virtual void findPath();
void moveTo(int x, int y);
void moveToPrint(int x, int y);
int getSteps(void);
void checkDirection();
};
Looks like you're trying to pass a non-static method to the std::thread constructor. You cannot do that: a non-static methods needs a object so it can be called. Looks like you want:
for(i = lRobs.begin(); i != lRobs.end(); i++)
{
vThreads.push_back(std::thread(&Crob::findPath, *i));
}

Issues passing an ellement of an Array of Records to Temporary Array of Records

I'm a new code monkey in training, and I'm currently having issues working with arrays and structs.
Currently I have a main file where I have a an Array of Records declared. I pass that array to an external function, where a quick sort is performed on the with of the fields in the record. Mainly the first name. I'm having an issues where I copy elements in the array of records, to a temporary array for the sorting algorith. I know that c++ does have a qsort function built in, but for what I'm working on right now, I need to have the algoritm written out the way it is. I was able to get this to work using only any array.
I'm getting the following error when trying to compile with the make file.
make
g++ -c -o main2.o main2.cpp
g++ -c externArray2.cpp -o externArray2.o
externArray2.cpp: In function ‘void copytemp(EmployeeRecord*, EmployeeRecord*, int, int)’:
externArray2.cpp:52: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:53: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:54: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:55: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:56: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:57: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:58: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:59: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:60: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
make: *** [externArray2.o] Error 1
Make File
test1: main.o ExternArray.o
g++ main.o ExternArray.o -o test1
externArray.o: ExternArray.cpp
g++ -c ExternArray.cpp -o ExternArray.o
main.o: main.cpp
g++ -c main.cpp -o main.o
Header.h
#ifndef _INCL_GUARD
#define _INCL_GUARD
const int maxEmployee =10;
const int NAMES = 5;
const int LENGTH = 15;
typedef struct EmployeeRecord
{
char first[10];
char last[10];
float reghours;
float ovrhours;
float pay;
float gross;
float defer;
float state;
float fed;
float ssi;
float net;
} EmployeeRecord;
#endif
main.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "./Header2.h"
void showArray(EmployeeRecord employees[], int, const char*); //Function Prototype 3.1
extern void qsortArray(EmployeeRecord employees[], int, int); //Funvtion Prototype 3.2
int main(void)
{
EmployeeRecord myEmployee[maxEmployee];
strcpy(myEmployee[0].first,"John");
strcpy(myEmployee[0].last,"Doe");
strcpy(myEmployee[1].first,"Ed");
strcpy(myEmployee[1].last, "Whittle");
strcpy(myEmployee[2].first, "Louise");
strcpy(myEmployee[2].last, "Marion");
strcpy(myEmployee[3].first,"Paula");
strcpy(myEmployee[3].last, "Prentiss");
strcpy(myEmployee[4].first, "Carl");
strcpy(myEmployee[4].last, "Davidson");
showArray(myEmployee, NAMES, "Before Sort");
qsortArray(myEmployee, 0, 4 );
showArray(myEmployee, NAMES, "After Sort");
return 0;
}
void showArray(EmployeeRecord employees[], int emp, const char *message)
{
cout << message << endl;
for (int test = 0; test < emp; test++)
{
cout << "First Name: " << employees[test].first << endl;
cout << "Last Name: " << employees[test].last << endl;
}
}
ExternArray.cpp
#include <cstring>
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
#include "./Header2.h"
void qsortArray(EmployeeRecord employees[], int, int); //Funvtion Prototype 3.2
void copytemp(EmployeeRecord tmpEmp[], EmployeeRecord emp[], int, int);
void qsortArray(EmployeeRecord employees[], int start, int finish)
{
int left=start,
right=finish;
char pivot[15];
strcpy(pivot, employees[(start+finish)/2].first);
while (left < right) {
cout << pivot << " pivot " << endl;
cout << "outer loop" << endl;
// find left candidate
while (strcmp(employees[left].first,pivot) <0) left++;
// find right candidate
cout << "First Inner Loop" << endl;
while (strcmp(employees[right].first,pivot) > 0 )right--;
cout << "Inner Loop" << endl;
if (left <= right)
{
EmployeeRecord tmpEmployee[1];
cout << "Create new struct" << endl;
copytemp(tmpEmployee, employees, 0, left);
cout << "copy to temp" << endl;
copytemp(tmpEmployee, employees, 1, right);
copytemp(employees, tmpEmployee, left, 1);
copytemp(employees, tmpEmployee, right, 0);
left++;
right--;
cout << "All copy done" <<endl;
}
} // while left < right
cout << "Back out of outer Loop" << endl;
if (start < right) qsortArray(employees,start,right);
if (left < finish) qsortArray(employees,left,finish);
}
void copytemp(EmployeeRecord tmpEmp[], EmployeeRecord emp[], int first, int secound)
{
memcpy(tmpEmp[first].first, emp[secound].first, sizeof(emp[secound].first));
memcpy(tmpEmp[first].last, emp[secound].last, sizeof(emp[secound].last));
memcpy(tmpEmp[first].reghours, emp[secound].reghours, sizeof(emp[secound].reghours));
memcpy(tmpEmp[first].ovrhours, emp[secound].ovrhours, sizeof(emp[secound].ovrhours));
memcpy(tmpEmp[first].pay, emp[secound].pay, sizeof(emp[secound].pay));
memcpy(tmpEmp[first].gross, emp[secound].gross, sizeof(emp[secound].gross));
memcpy(tmpEmp[first].defer, emp[secound].defer, sizeof(emp[secound].defer));
memcpy(tmpEmp[first].state, emp[secound].state, sizeof(emp[secound].state));
memcpy(tmpEmp[first].fed, emp[secound].fed, sizeof(emp[secound].fed));
memcpy(tmpEmp[first].ssi, emp[secound].ssi, sizeof(emp[secound].ssi));
memcpy(tmpEmp[first].net, emp[secound].net, sizeof(emp[secound].net));
}
In C you can copy an entire struct with a plain assignment
tmpElm[first] = emp[secound];
This is only problematic if the struct contains members which are pointers, which is not your case.
Try:
memcpy(&(tmpElm[first].first, &(emp[second].first), sizeof(emp[second].first));
The difference is that you need to pass an address of a float rather than a float value.
You may find that with C++ you need to cast the float pointer that the address of operator (&) gives you to a void, since C++ has stronger typing rules than C.
memcpy((void *)&(tmpElm[first].first, (void *)&(emp[second].first), sizeof(emp[second].first));
There are a few other ways to get the address of something when arrays are involved, but this is the simplest for a new programmer.