Error with Structs - c++

I am beginning to teach myself C++ and have come across an error for which I believe is quite simple but not catching it. I created the following header file named EmployeeT.h
#ifndef EMPLOYEET_H_INCLUDED
#define EMPLOYEET_H_INCLUDED
typedef struct
{
char firstInitial;
char middleInitial;
char lastInitial;
int employeeNumber;
int salary;
} EmployeeT
#endif // EMPLOYEET_H_INCLUDED
with the main as
#include <iostream>
#inclide <Employee.h>
using namespace std;
int main()
{
EmployeeT anEmployee;
anEmployee.firstInitial = 'M';
anEmployee.middleInitial = 'R';
anEmployee.lastInitial = 'G';
anEmployee.employeeNumber = 42;
anEmployee.salary = 80000;
cout << "Employee: " << anEmployee.firstInitial <<
anEmployee.middleInitial <<
anEmployee.lastInitial << endl;
cout << "Number: " << anEmployee.employeeNumber << endl;
cout << "Salary: " << anEmployee.salary <<endl;
return 0;
}

You missed semicolon:
typedef struct
{
char firstInitial;
char middleInitial;
char lastInitial;
int employeeNumber;
int salary;
} EmployeeT;
//^^Must not miss this ;
Meanwhile:
#inclide <Employee.h>
//^^typo
should be:
#include "Employee.h"
Last point: you may initialize your struct as follows:
anEmployee = {'M','R','G',42, 80000};
//It will assign values to field in automatic way
If you are curious, you may also take a look at uniform initialization which is introduced since C++11.

In main don't you want to #include "EmployeeT.h" instead of #include <Employee.h>?

Related

Is there a way to use google test on OOP program?

I'm new to google test and currently I'm writing a test for my OOP program, my OOP program is like this:
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
typedef unsigned int NUM;
class Employee
{
protected:
NUM MaSoThue;
private:
NUM Luong;
NUM CMND;
NUM a;
NUM b;
public:
Employee()
{
MaSoThue = 0;
Luong = 0;
CMND = 0;
}
Employee(NUM mst, NUM luong, NUM cmnd)
{
MaSoThue = mst;
Luong = luong;
CMND = cmnd;
}
//get
int getMaSoThue() const { return MaSoThue; }
int getLuong() const { return Luong; }
int getCMND() const {return CMND;}
//set
void setMaSoThue(NUM mst) {if (MaSoThue==0) MaSoThue = mst;}
void setLuong(NUM luong) {Luong = luong;}
void setCMND(NUM cmnd) {if (CMND==0) CMND = cmnd;}
};
int main()
{
// Objects
Employee PhucTri(111,222,333);
Employee MinhDang;
MinhDang.setMaSoThue(1234);
MinhDang.setLuong(2);
MinhDang.setCMND(8888);
//PhucTri
cout <<"MST cua Phuc Tri: "<< PhucTri.getMaSoThue()<<"\n";
cout << "Luong cua Phuc Tri: " << PhucTri.getLuong() << "\n";
cout << "CMND cua Phuc Tri: " << PhucTri.getCMND() << "\n\n";
//MinhDang
cout << "MST cua Minh Dang: " << MinhDang.getMaSoThue() << "\n";
cout << "Luong cua Minh Dang: " << MinhDang.getLuong() << "\n";
cout << "CMND cua Minh Dang: " << MinhDang.getCMND() << "\n";
}
I created a new file, which is below:
#include <gtest/gtest.h>
#include "FileCode.cc"
int main(){}
TEST(No1, PhucTri){
EXPECT_EQ(PhucTri.getMaSoThue(),111);
}
The compiler says that the object "PhucTri" isn't declared in this scope, but I did create it in my first file, is there any way I can get it right on the object ?
In general, Try to not include .cpp files, declare your class and its methods inside a .h file, define methods in .cpp and then create a test file that includes your header.
You have two options here, either define a test class that has an instance of your class follows instructions here.
Or do something like this :
#include <gtest/gtest.h>
#include "FileCode.h"
TEST(No1, PhucTri)
{
Employee PhucTri(111,222,333);
// initialize your data
// ......
EXPECT_EQ(PhucTri.getMaSoThue(),111);
}

Requesting a function from another file in a class not working

I have 2 .cpp files called "FactoringProgram.cpp" and "FactoringProgram2nd.cpp", also 1 header file called "FactoringProgram.h". I've already tried searching for this problem on StackOverflow and a couple other sites and haven't found a solution that worked for me. When I try to run this command: "g++ FactoringProgram.cpp FactoringProgram2nd.cpp" I get this error:
FactoringProgram.cpp: In function ‘int main()’: FactoringProgram.cpp:8:11: error: request for member ‘InitialMessage’
in ‘Problem1’, which is of non-class type ‘Factoring()’
Problem1.InitialMessage();
The code for "FactoringProgram.h" is:
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
int InitialMessage();
};
#endif
FactoringProgram.cpp code:
#include <stdio.h>
#include <iostream>
#include "FactoringProgramH.h"
int main()
{
Factoring Problem1();
Problem1.InitialMessage();
return 0;
}
FactoringProgram2nd.cpp code:
#include "FactoringProgramH.h"
#include <stdio.h>
#include <iostream>
Factoring::Factoring(int FirstCoefficent=0, char FirstOperator='+',
int SecondCoefficent=1, char SecondOperator='+', int 3rdExpression=1)
: m_FirstCoefficent(FirstCoefficen), m_FirstOperator(FirstOperator),
m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),
m_3rdExpression(3rdExpression);
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< FirstCoefficent << "x^2 " << FirstOperator << " " << SecondCoefficent
<< " x" << SecondOperator << " " << 3rdExpression;
}
Your code has several small errors. Here is a version that works (you can compare and see what I had to change). A summary of the changes are:
default parameter values should be in the header file (*.h);
you cannot start variable/argument names with numbers;
int InitialMessage() doesn't return anything, so I changed it to void InitialMessage();
when you initialize fields in a constructor, the last field cannot end with a semi-colon;
fixed the most vexing parsing (per #RSahu comment) in main.cpp.
FactoringProgram.h
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring(int FirstCoefficent = 0, char FirstOperator = '+',
int SecondCoefficent = 1, char SecondOperator = '+', int thirdExpression = 1);
void InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
Factoring::Factoring(int firstCoefficent, char firstOperator, int SecondCoefficent, char SecondOperator, int thirdExpression) :
m_FirstCoefficent(firstCoefficent),
m_FirstOperator(firstOperator),
m_SecondCoefficent(SecondCoefficent),
m_SecondOperator(SecondOperator),
m_3rdExpression(thirdExpression)
{}
void Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include "FactoringProgram.h"
int main()
{
Factoring Problem1;
Problem1.InitialMessage();
return 0;
}
To this error substitute:
Factoring Problem1();
by:
Factoring Problem1 = Factoring();
The problem is that the compiler is interpreting this line as a function declaration rather than a variable declaration. AS mentioned in the comments, this is known as the most vexing parser problem.
Obs: The code you posted contain many more minor errors.
You should define Factoring() without params and I used headers in FactoringProgram.h
FactoringProgram.h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
Factoring(int,char,int,char,int);
int InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
int main()
{
Factoring Problem1 = Factoring();
Problem1.InitialMessage();
system("pause");
return 0;
}
FactoringProgram2nd.cpp
#include "FactoringProgram.h"
Factoring::Factoring()
{
*this = Factoring(0, '+', 1, '+', 1);
}
Factoring::Factoring(int FirstCoefficent = 0, char FirstOperator = '+',int SecondCoefficent = 1, char SecondOperator = '+', int _3rdExpression = 1) : m_FirstCoefficent(FirstCoefficent), m_FirstOperator(FirstOperator),m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),m_3rdExpression(_3rdExpression)
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
return 0;
}

Whats the error in it. string assignment

I would like to take the data from the struct elements to the internal elements.
What will be a better way to do it.
It shows error: invalid array assignmen berror: invalid array ssignment
error: invalid array assignment error: ‘strcpy’ was not declared in this scope.
#include <iostream>
#include <string>
using namespace std;
struct A
{
char Ip[16];
char port[6];
char sessionkey[32];
}
int main()
{
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
strcpy(m_ip,a.Ip);
strcpy(m_port,a.port);
strcpy(m_sessionkey,a.sessionkey);
cout << "m_ip:" << m_ip << endl;
cout << "m_port:" << m_port << endl;
cout << "m_sessionkey:" << m_sessionkey << endl;
}
I think you mean the following (C string functions are declared in header <cstring>)
#include <cstring>
//...
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
std::strcpy(m_ip,a.Ip);
std::strcpy(m_port,a.port);
std::strcpy(m_sessionkey,a.sessionkey);
Or instead of
A a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
you could write
A a;
a = { "10.43.160.94111", "12345", "12Abcd12345Abcd12345Abcd1234512" };
provided that your compiler supports C++ 2011.
Take into account that you forgot to place a semicol after the closing brace in the structure definition
struct A
{
//...
};
^^^
EDIT: After you unexpectedly changed your code I'd like to point out that this code snippet
A a;
string p = "10.43.160.94111";
string q = "12345";
string r = "12Abcd12345Abcd12345Abcd1234512";
p.copy(a.Ip,16,0);
q.copy(a.port,6,0);
r.copy(a.sessionkey,32,0);
does not make sense. There is no sense to introduce objects of type std::string only that to initialize an object of type struct A.
Another thing you could initially define the structure the following way
struct A
{
std::string Ip;
std::string port;
std::string sessionkey;
};
For writing in C++ prefer to use std::string instead of char * or char[].
A number of your issues will not longer exist if you use std::string instead.
Example:
#include <iostream>
#include <string>
struct A
{
std::string Ip;
std::string port;
std::string sessionkey;
};
int main()
{
std::string m_ip;
std::string m_port;
std::string m_sessionkey;
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
// copy data from a to local variables
m_ip = a.Ip;
m_port = a.port;
m_sessionkey = a.sessionkey;
std::cout << "m_ip:" << m_ip << std::endl;
std::cout << "m_port:" << m_port << std::endl;
std::cout << "m_sessionkey:" << m_sessionkey << std::endl;
}
If you insist on using strcpy you must include the C header file string.h either by using #include <string.h> or by using #include <cstring>. Note that this is a C header file and it is distinctly different than the C++ #include <string> header file.
You should change your code like this:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct A
{
char* Ip;
char* port;
char* sessionkey;
};
int main()
{
char m_ip[16];
char m_port[6];
char m_sessionkey[32];
A a;
a.Ip = "10.43.160.94111";
a.port = "12345";
a.sessionkey = "12Abcd12345Abcd12345Abcd1234512";
strcpy(m_ip,a.Ip);
strcpy(m_port,a.port);
strcpy(m_sessionkey,a.sessionkey);
cout << "m_ip:" << m_ip << endl;
cout << "m_port:" << m_port << endl;
cout << "m_sessionkey:" << m_sessionkey << endl;
}
strcpy() function is in cstring header file in C++/C++11, so you must add #include<cstring> to your code.

C++ : Turn char array into a string that is printed

just a beginner student learning basic C++. I'm trying to figure out the best way to:
Turn a char array Name of 20 into a string that can be printed.
I found in other Stack Overflow topics to use "str()" such as "str(Name)", but it always comes up 'identifier not found'.
cout << "Name:" << str(Name) << endl;
Set a char array of 20 characters. For some reason, the following gives me errors when declaring. I've tweaked it so many times, but I cannot get why it won't give.
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
Full code I have so far:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
//Step 1
struct StudentRecord
{
char Name[20];
//Accessor
void printInfo() const;
};
void StudentRecord::printInfo() const
{
cout << "Name:" << str(Name) << endl;
}
int main()
{
//Step 2
StudentRecord TESCStudent;
TESCStudent.Name[20] = {'S','u','p','e','r','P','r','o','g','r','a','m','m','e','r','\0'};
//Step 3
TESCStudent.printInfo();
_getch();
return 0;
}
Given that you are at a very beginner level, just use std::string:
#include <iostream>
#include <conio.h>
#include <string>
struct StudentRecord {
std::string Name;
void printInfo() const {
std::cout << "Name:" << Name << '\n';
}
};
int main() {
StudentRecord TESCStudent;
TESCStudent.Name = "SuperProgrammer";
TESCStudent.printInfo();
_getch();
}
Live demo
The syntax like this:
char Name[20] = {'S','u','p','e','r','\0'};
is used to initialize a variable when you define it. However, in your case,
StudentRecord TESCStudent;
TESCStudent.Name[20] = ...;
You've already defined it on the line before, so you can't "initialize", you have to "assign" it.
This is pretty much why you use std:string instead of char[].

Can't return value from a function call to main() correctly

I am trying to test this simple function, but Opt.status, Opt.Year values are not returned back to main(). Why? Please help as I am new to C++.I am using visual c++ to execute these codes.This is in my .cpp file
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdio.h"
using namespace std;
int main()
{
Easy_Task obj_EasyTask;
TimeDateMonthOptions whatOptions=DATE;
TOptions Opt;
Opt.status=FALSE;
Opt.Year=1970;
printf("Enter code\n");
scanf("%d",&obj_EasyTask.code);
cout << "the code entered is: " << obj_EasyTask.code;
obj_EasyTask.display2(obj_EasyTask.code);
cout << "\nOutput: " << obj_EasyTask.show();
printf("\nEnter the options that you prefer\n");
scanf("%d",&whatOptions);
obj_EasyTask.display3(whatOptions, Opt);
cout << "\nOpt.Year: " << Opt.Year;
if(Opt.status)
{
obj_EasyTask.x=(Opt.Year)& 0x00FF;
obj_EasyTask.y=((Opt.Year)& 0xFF00)>>8;
cout << "\nX: " << obj_EasyTask.x;
cout << "\nY: " << obj_EasyTask.y;
obj_EasyTask.Result=(obj_EasyTask.x)*(obj_EasyTask.y);
}
char holdWindow;
std::cin >> holdWindow;
return 0;
}
uint16_t Easy_Task::display2(uint16_t code)
{
if(code==1)
{
c = 7;
}
else
{
c = 9;
}
return c;
}
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions Opt)
{
switch(whtOptions)
{
case 0:
case 1:
case 2:
case 3:
Opt.status=TRUE;
cout << "\nStatus1: " << Opt.status;
Opt.Year=1991;
cout << "\nYear1: " << Opt.Year;
break;
case 7:
Opt.status=FALSE;
cout << "\nStatus2: " << Opt.status;
Opt.Year=2013;
cout << "\nYear2: " << Opt.Year;
break;
default:
Opt.status=FALSE;
cout << "\nStatus3: " << Opt.status;
Opt.Year=2010;
cout << "\nYear3: " << Opt.Year;
break;
}
return Opt.status, Opt.Year;
}
In my .h file I have the class defined as follows:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
typedef unsigned short uint16_t;
#define TRUE 1;
#define FALSE 0;
typedef struct TOptions
{
bool status;
uint16_t Year;
};
typedef enum
{
YEAR,
MONTH,
DATE,
HOURS,
MINUTES,
SECONDS,
HUNDRETHS,
UNDEFINED
}TimeDateMonthOptions;
class Easy_Task
{
public:
uint16_t code, c, x,y, Result;
uint16_t display2(uint16_t code);
uint16_t show()
{
return c;
};
uint16_t display3(TimeDateMonthOptions whatOptions, TOptions Opt);
};
The problem I have is line:
if(Opt.status)
Where it doesn't return the value of 1 but instead in takes the default value which was defined earlier. Why is this happening?
You need to pass the argument by reference:
uint16_t Easy_Task::display3(TimeDateMonthOptions whtOptions, TOptions& Opt)
//^
Otherwise, a copy of Opt is made and modified in the function and the caller will never see the changes.
Note that:
return Opt.status, Opt.Year;
does not somehow return two values. This is using the comma operator and will return the value Opt.Year. However, if you pass Opt by reference a return value is unrequired.
Better still define a function to return TOptions
TOptions Easy_Task::display3(...); and return a structure.
Remember that you can only ever return ONE SINGLE return value from the function.