[英]Xcode linker error: linker command failed with exit code 1 (use -v to see invocation)
Using C++ and Xcode I always run into this problem where linker command fails. 使用C ++和Xcode,我总是遇到链接器命令失败的问题。 I don't know what this means. 我不知道这是什么意思。 I'm creating an elementary program that consists of 3 files. 我正在创建一个包含3个文件的基本程序。 A main.cpp, and a class file that has a header. 一个main.cpp,以及一个带有头文件的类文件。 The main file is empty. 主文件为空。 This what the other two look like: 其他两个看起来像这样:
student.hpp : student.hpp:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <sstream>
std::string line;
class Student{
public:
std::string fullName;
int projectGrade;
int quizGrade;
int midtermGrade;
int finalGrade;
int finish;
int start = 14;
std::string getStudentName();
int getProjectGrade();
int getQuizGrade();
int getMidtermGrade();
int getFinalGrade();
double getOverallGrade();
bool login(std::string username, std::string password);
bool login(std::string username);
};
student.cpp : student.cpp :
#include "student.hpp"
std::string Student::getStudentName(){
finish = line.find("\t", start);
fullName = line.substr(start, finish);
return fullName;
}
int Student::getProjectGrade(){
start = line.find("\t", start) + finish;
finish = start + 2;
std::stringstream stream(line.substr(start, 2));
stream >> projectGrade;
return projectGrade;
}
int Student::getQuizGrade(){
start+=3;
std::stringstream stream(line.substr(start, 2));
stream >> quizGrade;
return quizGrade;
}
int Student::getMidtermGrade(){
start+=3;
std::stringstream stream(line.substr(start, 2));
stream >> midtermGrade;
return midtermGrade;
}
int Student::getFinalGrade(){
start+=3;
std::stringstream stream(line.substr(start, 2));
stream >> finalGrade;
return finalGrade;
}
double Student::getOverallGrade(){
return round((projectGrade + quizGrade + midtermGrade + finalGrade)/40)*10;
}
bool Student::login(std::string username, std::string password){
std::ifstream studentFile;
studentFile.open("/Users/griffin/desktop/Data Structures/Data Structures1/Data Structures1/Students.txt");
if(studentFile.is_open()){
while(getline (studentFile,line))
{
if ((username.length() == 7 && username.substr(0,4).compare("u000") == 0 && line.find(username) != std::string::npos) && (password.length() == 6 && password.substr(0,2).compare("pw") == 0 && line.find(password) != std::string::npos))
return true;
}
}
return false;
}
bool Student::login(std::string username){
std::ifstream studentFile;
studentFile.open("/Users/griffin/desktop/Data Structures/Data Structures1/Data Structures1/Students.txt");
if(studentFile.is_open()){
while(getline (studentFile,line))
{
if (username.length() == 7 && username.substr(0,4).compare("u000") == 0 && line.find(username) != std::string::npos)
return true;
}
}
return false;
}
The main file includes student.hpp. 主文件包括student.hpp。 Is this a problem on Xcode's end or do I have an error in my code? 这是Xcode的问题,还是我的代码有错误?
Main.cpp Main.cpp的
#include "student.hpp"
int main(int argc, const char * argv[]) {
return 0;
}
Your problem is that you've multiply declared the global variable line
. 您的问题是您已经多次声明了全局变量line
。
There is no good reason for a global variable in your code so the simple solution is to declare line
inside the two login methods instead. 您的代码中没有全局变量的充分理由,因此简单的解决方案是在两个登录方法中声明line
。
For future reference however, here's how you should declare global variables 为了将来参考,这里是您应该如何声明全局变量的方法
How do I use extern to share variables between source files? 如何使用extern在源文件之间共享变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.