I am new to OOP/C++. I have three files: main.cpp, matrix.cpp and matrix.h. I am getting errors all the time, it sasys
error C2653: 'matrix' : is not a class or namespace name
error C2065: 'tomb' : undeclared identifier
main.cpp:
#include "stdafx.h"
#include "matrix.h"
int _tmain(int argc, _TCHAR* argv[])
{
matrix m;
return 0;
}
matrix.h:
#ifndef MATRIX_H
#define MATRIX_H
class matrix{
private:
int tomb[3][3];
public:
matrix(); //default konstruktor
};
#endif
matrix.cpp
#include "matrix.h"
#include "stdafx.h"
#include <iostream>
matrix::matrix(){
unsigned int x=0, y=0;
for(unsigned int i = 0; i<9; i++){
std::cout << "Kerem a(z) " << i << ". szamot" << std::endl;
std::cin >> matrix::tomb[x][y];
if(y<2) y++;
else{
y =0;
x++;
}
}
}
Related
I'm a beginner and I want to use instance w of OlMainWindow in order to access _x by pressing address (OlMain->vref->_x=10;) through ui_MainWidow::setupUI() function, but when I compile this program then I get an error on line OlMain->vref->_x=10:
error: use of undefined type 'OlMainWindow'
Please help me fix this error.
ui_MainWidow.h
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
#include <iostream>
#include "olvalue.h"
class OlValue;
class OlMainWindow;
class ui_MainWidow;
class ui_MainWidow {
public:
void setupUI(OlMainWindow* OlMain) {
OlMain->vref->_x=10; // _x = 10;
OlMain->vref->_x = 10;
std::cout << " ok";
}
};
#endif //UI_MAINWINDOW_H
olvalue.h
#include <iostream>
#ifndef VALUE_H
#define VALUE_H
#include "ui_mainwindow.h"
class OlValue;
class OlMainWindow;
class ui_MainWidow;
class OlValue
{
public:
int _x;
int _y;
public:
OlValue() {
_x = 10;
_y = 20;
}
};
#endif //VALUE_H
olmainwindow.h
#ifndef OLMAINWINDOW_H
#define OLMAINWINDOW_H
#include <iostream>
#include <string>
#include "olvalue.h"
class OlValue;
class OlMainWindow;
class ui_MainWidow;
class OlMainWindow
{
public:
OlValue* vref = new OlValue;
OlMainWindow();
~OlMainWindow();
void enterUsername();
private:
int val;
std::string _Username;
};
#endif //OLMAINWINDOW_H
olmainwidow.cpp
#include "olmainwindow.h"
#include <iostream>
#include <string>
class OlValue;
class OlMainWindow;
class ui_MainWidow;
OlMainWindow::OlMainWindow() {
ui_MainWidow* ui = new ui_MainWidow();
ui->setupUI(this);
}
OlMainWindow::~OlMainWindow() {
//delete ui;
}
void OlMainWindow::enterUsername() {
std::cout << "Please, enter Username: ";
std::getline(std::cin, _Username);
std::cout << "You'v accessed Username: " << _Username << std::endl;
}
main.cpp
#include "olmainwindow.h"
#include <iostream>
int main(int argc, char* argv[])
{
OlMainWindow* w = new OlMainWindow();
return 0;
}
In ui_MainWidow.h, the implementation of setupUI() is inlined, but when it tries to access the members of OlMainWindow it fails because the OlMainWindow class hasn't actually been defined yet, merely forward declared.
Your files are not setup correctly. You are creating dependencies where they don't belong, and then you are trying to use forward declarations to solve circular dependencies issues that shouldn't exist in the first place.
Try this instead:
OlValue.h
#ifndef OLVALUE_H
#define OLVALUE_H
class OlValue
{
public:
int _x = 10;
int _y = 20;
};
#endif //OLVALUE_H
ui_MainWindow.h
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
class OlMainWindow;
class ui_MainWindow {
public:
void setupUI(OlMainWindow* OlMain);
};
#endif //UI_MAINWINDOW_H
ui_MainWindow.cpp
#include "ui_MainWindow.h"
#include "OlMainWindow.h"
#include <iostream>
void ui_MainWindow::setupUI(OlMainWindow* OlMain) {
OlMain->vref->_x = 10;
OlMain->vref->_x = 10;
std::cout << " ok";
}
olMainWindow.h
#ifndef OLMAINWINDOW_H
#define OLMAINWINDOW_H
#include "OlValue.h"
#include <string>
class ui_MainWidow;
class OlMainWindow
{
public:
OlValue* vref = new OlValue;
OlMainWindow();
~OlMainWindow();
void enterUsername();
private:
ui_MainWidow* ui;
int val;
std::string _Username;
};
#endif //OLMAINWINDOW_H
olMainWindow.cpp
#include "olMainWindow.h"
#include <iostream>
#include <string>
OlMainWindow::OlMainWindow() {
ui = new ui_MainWindow();
ui->setupUI(this);
}
OlMainWindow::~OlMainWindow() {
delete ui;
}
void OlMainWindow::enterUsername() {
std::cout << "Please, enter Username: ";
std::getline(std::cin, _Username);
std::cout << "You'v accessed Username: " << _Username << std::endl;
}
main.cpp
#include "olMainWindow.h"
#include <iostream>
int main(int argc, char* argv[])
{
OlMainWindow w;
w.enterUsername();
return 0;
}
I'm trying to pass a vector to to a function as a reference so that I can print the contents. The problem is the following compiling errors.
Print.h:7:19: error: variable or field ‘print_stuff’ declared void
void print_stuff(vector<int> &month_mileage);
^~~~~~
Print.h:7:19: error: ‘vector’ was not declared in this scope
Print.h:7:26: error: expected primary-expression before ‘int’
void print_stuff(vector<int> &month_mileage);
main.cpp: In function ‘int main()’:
main.cpp:20:12: error: ‘print_stuff’ is not a member of ‘Print’
Print::print_stuff(&month_mileage);
^~~~~~~~~~~
Print.cpp:4:19: error: variable or field ‘print_stuff’ declared void
void print_stuff(vector<int> &month_mileage) {
^~~~~~
Print.cpp:4:19: error: ‘vector’ was not declared in this scope
Print.cpp:4:26: error: expected primary-expression before ‘int’
void print_stuff(vector<int> &month_mileage) {
^~~
I believe the problem could possibly be related to the way I have my files set up. From all the research I have done I can't find anything that has helped me other than to #include in the header file. Which I thought was bad practice, but I understand I could be wrong.
[main.cpp]
#include "Print.h"
#include <vector>
#include <iostream>
using namespace std;
using namespace Print;
int main() {
int num;
vector<int> month_mileage(0,12);
cout << " Please enter your mileage for the past 12 months\n";
for( int i = 0; i < 12; i++) {
cin >> num;
month_mileage[i] = num;
}
Print::print_stuff(month_mileage);
return 0;
}
[Print.h]---------------------------------------------------
0 #ifndef PRINT_H
#define PRINT_H
namespace Print {
void print_stuff(vector<int> &month_mileage);
};
#endif
[Print.cpp]---------------------------------------------------
namespace Print{
void print_stuff(vector<int> &month_mileage) {
for(int i = 0; i < 12; i++) {
for(int a = 0; a < &month_mileage[i]; a++) {
std::cout <<"|";
}//END FOR
std::cout <<'\n';
}//END OUTER FOR
}//END PRINT_STUFF
}
UPDATED CODE is as follows
[Print.h]---------------------------------------------------
#ifndef PRINT_H
#define PRINT_H
#include <vector>
namespace Print {
void print_stuff(std::vector<int> &month_mileage);
};
#endif
[Print.cpp]---------------------------------------------------
namespace Print{
void print_stuff(std::vector<int> &month_mileage) {
for(int i = 0; i < 12; i++) {
for(int a = 0; a < month_mileage[i]; a++) {
std::cout <<"|";
}
std::cout <<'\n';
}
}
}
[main.cpp]---------------------------------------------------
#include "Print.h"
#include <vector>
#include <iostream>
using namespace Print;
int main() {
int num;
std::vector<int> month_mileage(0,12);
std::cout << " Please enter your mileage for the past 12 months\n";
for( int i = 0; i < 12; i++) {
std::cin >> num;
month_mileage[i] = num;
}
Print::print_stuff(month_mileage);
return 0;
}
So my question is am I passing the reference incorrectly? or have I set up the Header file incorrectly?
Thanks
To solve the problem I had to
List item
add #include <vector> to the Print.h file
add #include <vector> and #include <iostream> to Print.cpp
pass reference as void print_stuff(std::vector<int> &month_mileage);
Note that unlike C arrays, c++ classes have to have & before them just like regular parameters you pass by, otherwise it will create a copy on the stack in the function it was passed to.
#include<bits/stdc++.h>
using namespace std;
int fun(vector<int> v)
{
//your code here
}
int main()
{
vector<int> v;
int n,x;
cin>>n;enter code here
for(int i=1;i<=n;i++)
v.push_back(x);
cout<<fun(v); // make it void if you need it void.
}
I have a problem. In my Qt project I'm having an error. Tt says:
multiple definition of estructura
I think it is because I'm using too many "#includes", but I don't know how to fix it.
This is my program:
Struct.h
#ifndef STRUCT_H
#define STRUCT_H
#include <stdio.h>
#include <string>
struct Circulo{
std::string nombre;
int capacidad;
int statusMujer[4];
int hijos[4];
};
struct Circulo estructura[30];
#endif // STRUCT_H
Logica.h
#ifndef LOGICA_H
#define LOGICA_H
#include <iostream>
void madresStatusCodificados(std::string , int , int[]);
void ListadoDeCirculos(int, int , std::string []);
std::string CirculoMayorCapacidad(int );
int NinnosEnCirculosconStatus(int , int );
void Listado(int);
#endif // LOGICA_H
interfaz.cpp
#include <Interfaz/Interfaz.h>
#include <Logica/Defecto.h>
#include <Interfaz/Menu.h>
#include <Logica/Struct.h>
#include <iostream>
using namespace std;
void Principal(){
cin>>estructura[i].statusMujer[3];
cout<<"Escriba la cantidad de hijos con madres desconocidas"<<endl;
cin>>estructura[i].hijos[3];
i++;
}else{
break;
}
}
}
Logica.cpp
#include <iostream>
using namespace std;
//#include <Logica/Logica.h>
#include <Logica/Struct.h>
void madresStatusCodificados(string t, int h , int k[]){
for(int i=0;i<h;i++){
if(estructura[i].nombre==t){
k[0]=estructura[i].statusMujer[0];
k[1]=estructura[i].statusMujer[1];
k[2]=estructura[i].statusMujer[2];
k[3]=estructura[i].statusMujer[3];
}
}
}
void ListadoDeCirculos(int g,int u,string h[]){
for(int i=0;i<u;i++){
if(estructura[i].statusMujer[g-1]!=0)
h[i]=estructura[i].nombre;
}
}
string CirculoMayorCapacidad(int k){
int n=-1;
string l;
for(int i=0;i<k;i++){
if(estructura[i].capacidad>n){
n=estructura[i].capacidad;
l=estructura[i].nombre;
}
}
return l;
}
int NinnosEnCirculosconStatus(int a,int b){
int h=0;
for(int i=0;i<b;i++){
h=h+estructura[i].hijos[a-1];
}
return h;
}
void Listado(int y){
for(int i=0;i<y;i++){
cout<<"NOMBRE DEL CIRCULO CAPACIDAD Madres solteras Madres trabajadoras Madres caso social Madres desconocidas "<<endl;
cout<<estructura[i].nombre<<""<<estructura[i].capacidad<<estructura[i].statusMujer[0]<<estructura[i].statusMujer[1]<<estructura[i].statusMujer[2]<<estructura[i].statusMujer[3]<<endl;
}
}
I am getting this error:
Errors
You have defined a variable named "estructura" in several files, the error tells you that it was first defined in Struct.h being called #included in Logica.cpp,
struct Circulo estructura[30];
you have another definition the the file Defecto.h or a file included by Defecto.h
Move the definition of estructura in a cpp file of your choice, then declare it as extern wherever you need it. And you can omit the struct keyword, in c++.
In a struct.cpp file:
#include "Struct.h"
Circulo estructura[30];
In your Logica.cpp and Interfaz.cpp files:
#include "Struct.h"
extern Circulo estructura[30];
Here is a long and detailed explanation of your issue.
I need to provide a CFG class in a separate file, but I'm unsure how to compile it together with the associated .h and the main program.
I've #includeed the .h file and I've asked for both files at the command line, but I'm not sure why this is wrong for compiling them together.
Thoughts?
CFG.cpp:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CFG
{
public:
string code[25];
char startNT;
//private:
CFG(string inCode[], int stringLen)
{
for (int a = 0; a < stringLen; a++)
{
//cout << inCode[a] << endl;
this->code[a] = inCode[a];
}
for (int a = 0; a < stringLen; a++)
{
cout << this->code[a] << endl;
}
}
char getStartNT()
{
return startNT;
}
void setStartNT(char stNT)
{
startNT = stNT;
}
bool processData(string inString, string wkString)
{
//Our recursive function
return true;
}
void garbage()
{
return;
}
};
CFG.h:
#ifndef _cfg_h_
#define _cfg_h_
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CFG
{
public:
string code[25];
char startNT;
CFG(string inCode[], int stringLen);
char getStartNT();
void setStartNT(char stNT);
bool ProcessData(string inString, string wkString);
void garbage();
};
#endif
cfg_entry.cpp:
#include <stdio.h>
#include <iostream>
#include "cfg.h"
using namespace std;
int main()
{
string inArray[5];
inArray[0] = "test0";
inArray[1] = "test1";
inArray[2] = "test2";
inArray[3] = "test3";
inArray[4] = "test4";
CFG * cfg1 = new CFG(inArray, 5);
cfg1->garbage();
return 0;
}
Compile errors:
art#tv:~/Dropbox/Weber/CS 4110/Individual Assignment 2$ g++ -g -std=c++11 -Wall -o cfg_entry cfg.cpp cfg_entry.cpp
/tmp/ccICQEd0.o: In function `main':
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:15: undefined reference to `CFG::CFG(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)'
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:16: undefined reference to `CFG::garbage()'
collect2: error: ld returned 1 exit status
I found my issue. In my case, the header file was defining the class and the .cpp file was re-defining it again, trying to create 2 instances of the CFG class. The .h needed to handle the class declaration and variable instantiation while the .cpp handles only the function definitions.
cfg.h:
#ifndef _cfg_h_
#define _cfg_h_
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CFG
{
private:
string code[25];
char startNT;
public:
CFG(string inCode[], int stringLen);
char getStartNT();
void setStartNT(char stNT);
bool processData(string inString, string wkString);
void garbage();
};
#endif
cfg.cpp:
#include <iostream>
#include <stdio.h>
#include <string>
#include "cfg.h"
using namespace std;
CFG::CFG(string inCode[], int stringLen)
{
for (int a = 0; a < stringLen; a++)
{
//cout << inCode[a] << endl;
this->code[a] = inCode[a];
}
for (int a = 0; a < stringLen; a++)
{
cout << this->code[a] << endl;
}
}
char CFG::getStartNT()
{
return startNT;
}
void CFG::setStartNT(char stNT)
{
startNT = stNT;
}
bool CFG::processData(string inString, string wkString)
{
//Our recursive function
return true;
}
void CFG::garbage()
{
return;
}
Im on year 10 and our teacher wants us to create an original project and using pointers
What I want to do is to create Members and be able to sort the members by there names and print them
When I run my code it says Invalid Access
Team.h
#ifndef TEAM_H
#define TEAM_H
#include "Staff.h"
#include <vector>
#include <iostream>
using std::vector;
class Team: public Staff
{
public:
Team();
~Team();
vector<Staff *> &getVector();
private:
vector<Staff *> myStaffs;
};
#endif // TEAM_H
Team.cpp
Team::Team()
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
vector<Staff*>& Team::getVector()
{
return myStaffs;
}
Command class will do the sorting of team and print all team members
Command.cpp
void Command::printStaffs(vector<Staff*>&myStaffs)
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
std::cout << "Staff ID number: "<< myStaffs[iStaff]->getStaId() << std::endl
<< "Staff Skills 1: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< "Staff Skills 2: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< "Staff Skills 3: " << *myStaffs[iStaff]->getStaSkill() << std::endl
<< std::endl;
}
}
Command.h
#ifndef CommandH
#define CommandH
#include "Team.h"
#include <vector>
#include <iostream>
using std::vector;
class Command: public Team
{
public:
Command(){}
~Command(){}
void sortVector(vector<Staff* >&vectorTemp);
void printStaffs(vector<Staff* >&);
private:
vector<Staff *> vectEmployee;
};
//--------------------------------------------------------------------------
#endif
main.cpp
#include <iostream>
#include <conio.h>
#include "Team.h"
#include "Command.h"
int main()
{
Team t;
Command c;
c.printStaffs(t.getVector());
getch();
return 0;
}
Staff.h
#ifndef STAFF_H
#define STAFF_H
#include <cstdlib>
#include <ctime>
#include <string>
using std::rand;
class Staff
{
public:
Staff();
~Staff();
static Staff* createStaff(int); // creates staffs
int** getStaSkill();
int getStaId(); // returns Staff ID
static int genRanNum(int); //Generate random number
private:
int *staSkill[3];
int staId;
//int staDeptAsigned;
};
#endif
Staff.cpp
#include "Staff.h"
Staff::Staff()
{
*staSkill = new int[3];
}
Staff *Staff::createStaff(int s)
{
Staff *staff = new Staff();
staff->staId = s;
*(staff->staSkill[0]) = genRanNum(10);
*(staff->staSkill[1]) = genRanNum(10);
*(staff->staSkill[2]) = genRanNum(10);
return staff;
}
int** Staff::getStaSkill()
{
return staSkill;
}
int Staff::getStaId()
{
return staId;
}
int Staff::genRanNum(int num)
{
return 1 +(std::rand()%num);
}
Staff::~Staff(){}
When you construct a Team, you have the following constructor:
Team::Team()
{
for(unsigned int iStaff = 0; iStaff < myStaffs.size(); iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
However, myStaffs is a member of Team and gets default constructed as empty, so nothing happens here since myStaffs.size() == 0.
Calling printStaffs on this Team::getVector() will correctly inform you that the vector is empty:
int main()
{
Command c;
Team t; // t.myStaffs will be empty
c.printStaffs(t.getVector()); // passes an empty vector to printStaffs
return 0;
}
You might want to pass a number to your Team constructor to create that many staffs:
Team::Team(int number_of_staff)
{
for(unsigned int iStaff = 0; iStaff < number_of_staff; iStaff++)
{
myStaffs.push_back(createStaff(iStaff));
}
}
int main()
{
Command c;
Team t(5); // t.myStaffs will contain 5 staff members
c.printStaffs(t.getVector()); // passes vector of 5 staff
return 0;
}