如何解决将代码分离到头文件时的问题
这个程序可以正常工作,但是当我将“Class StudentsName”分开并将其放在头文件中时,它无法正常工作。我通过右键单击标题文件夹并选择新项目和选择标题将头文件添加到我的项目中,但它无法正常工作。请帮助我!
主文件:
#include "iostream"
#include "string"
#include "Students.h"
using namespace std;
int main()
{
string nameOfStudent;
StudentsName myStudentsName(" The student name is: Jason");
cout<<myStudentsName.getMyName()<<endl;
cout<<"please enter the name of the student: "<<endl;
getline(cin,nameOfStudent);
myStudentsName.setMyName(nameOfStudent);
cout<<endl;
myStudentsName.displayMyName();
system("pause");
return 0;
}
头文件(Students.h):
class StudentsName
{
public:
StudentsName (string stdName)
{
setMyName(stdName);
}
void setMyName(string stdName)
{
myName=stdName;
}
string getMyName ()
{
return myName;
}
void displayMyName()
{
cout<<"The student name is: "<<getMyName()<<endl;
}
private:
string myName;
};
错误:
students.h(5): error C2061: Syntax error : identifier 'string'
students.h(9): error C2061: Syntax error : identifier 'string'
students.h(13): error C2146: Syntax error : missing ';' before identifier 'getMyName'
students.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
students.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
students.h(16): warning C4183: 'getMyName': missing return type; assumed to be a member function returning 'int'
students.h(23): error C2146: Syntax error : missing ';' before identifier 'myName'
students.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
students.h(7): error C2065: 'stdName' : undeclared identifier
students.h(11): error C2065: 'myName' : undeclared identifier
students.h(11): error C2065: 'stdName' : undeclared identifier
students.h(15): error C2065: 'myName' : undeclared identifier
students.h(19): error C2065: 'cout' : undeclared identifier
students.h(19): error C2065: 'endl' : undeclared identifier
studentname.cpp(11): error C2664: 'StudentsName::StudentsName(const StudentsName &)' : cannot convert parameter 1 from 'const char [84]' to 'const StudentsName &'
1> Reason: cannot convert from 'const char [84]' to 'const StudentsName'
1> No constructor Could take the source type,or constructor overload resolution was ambiguous
studentname.cpp(16): error C2660: 'StudentsName::setMyName' : function does not take 1 arguments
解决方法
using 指令
data "aws_caller_identity" "current" {}
resource "aws_iam_role" "external_dns_role" {
name = "external-dns"
assume_role_policy = jsonencode({
"Version": "2012-10-17","Statement": [
{
"Effect": "Allow","Principal": {
"Federated": format(
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:%s",replace(
"${aws_eks_cluster.<YOUR_CLUSTER_NAME>.identity[0].oidc[0].issuer}","https://","oidc-provider/"
)
)
},"Action": "sts:AssumeRoleWithWebIdentity","Condition": {
"StringEquals": {
format(
"%s:sub",trimprefix(
"${aws_eks_cluster.<YOUR_CLUSTER_NAME>.identity[0].oidc[0].issuer}","https://"
)
) : "system:serviceaccount:default:external-dns"
}
}
}
]
})
}
放置在包含标题 using namespace std;
"Student.h"
因此名称 #include "iostream"
#include "string"
#include "Students.h"
using namespace std;
在标题 string
中未定义。
将指令放在包含标题之前。
"Students.h"
尽管在标头 #include "iostream"
#include "string"
using namespace std;
#include "Students.h"
中包含标头 <string>
和 <iostream>
并使用诸如 "Students.h"
之类的限定名称会更好。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。